정적, 캡슐화 및 상속을 지원하는 Python 용 강력한 Java 스타일 OOP 시스템.
Kento Nishi와 Ronak Badhe의 PYPI / 제작
Python ++를 사용하면 Python 프로그래머가 Python에서 객체 지향 프로그래밍 원리를 사용할 수 있습니다.
패키지는 PYPI에서 사용할 수 있습니다. 다음 명령으로 패키지를 설치할 수 있습니다.
pip install pythonpp와일드 카드 가져 오기 명령문을 사용하여 Python ++를 가져올 수 있습니다.
from pythonpp import * @PythonPP 데코레이터로 Python ++ 클래스를 선언하십시오.
@ PythonPP
class MyClass :
pass # class code here namespace 내에서 파이썬 ++ 클래스에 대한 변수 및 메소드를 선언합니다.
@ PythonPP
class MyClass :
def namespace ( public , private ):
pass # methods and variables here namespace 내 코드는 다음 범위에 액세스 할 수 있습니다.
| 범위 | 설명 |
|---|---|
public | 공개 인스턴스 범위. |
private | 개인 인스턴스 범위. |
public.static | 공개 정적 범위. |
private.static | 개인 정적 범위. |
@staticinit 데코레이터를 사용하여 Python ++ 클래스의 정적 초기화 선언. 정적 초기화기는 인스턴스 변수 및 메소드에 액세스 할 수 없습니다. 정적 초기화기에는 입력 매개 변수가 없습니다.
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ staticinit
def StaticInit ():
public . static . publicStaticVar = "Static variable (public)"
private . static . privateStaticVar = "Static variable (private)" 또는 변수 할당이 일정하면 정적 변수를 베어 namespace 에서 선언 할 수 있습니다. 베어 정적 변수 선언을 사용하는 것은 권장되지 않습니다 .
생성자는 @constructor 데코레이터를 사용하여 선언 할 수 있습니다. 생성자는 입력 매개 변수를 가질 수 있습니다.
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ constructor
def Constructor ( someValue ):
public . publicInstanceVar = "Instance variable (public)"
public . userDefinedValue = someValue 메소드는 namespace 에 public 및 private 스코프가있는 @method(scope) 데코레이터를 사용하여 선언됩니다.
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ method ( public )
def publicMethod ():
pass # public instance method here
@ method ( private )
def privateMethod ():
pass # private instance method here
@ method ( public . static )
def publicStaticMethod ():
pass # public static method here
@ method ( private . static )
def privateStaticMethod ():
pass # private static method here @special 데코레이터를 사용하여 특별한 내장 방법을 선언하십시오.
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ special
def __str__ ():
return "Some string value"클래스는 표준 파이썬 클래스 상속을 사용하여 다른 클래스를 확장 할 수 있습니다.
@ PythonPP
class ParentClass :
def namespace ( public , private ):
@ staticinit
def StaticInit ():
public . static . staticVar = "Static variable"
@ constructor
def Constructor ( param ):
print ( "Parent constructor" )
public . param = param
@ PythonPP
class ChildClass ( ParentClass ): # ChildClass extends ParentClass
def namespace ( public , private ):
@ staticinit
def StaticInit ():
ParentClass . staticinit () # Call parent static initializer
public . static . staticVar2 = "Static variable 2"
@ constructor
def Constructor ( param ):
# Call parent constructor
ParentClass . constructor ( param ) from pythonpp import *
@ PythonPP
class ParentClass :
def namespace ( public , private ):
@ staticinit
def StaticInit ():
public . static . publicStaticVar = "Public static variable"
private . static . privateStaticVar = "Private static variable"
@ constructor
def Constructor ( parameter ):
private . privateVariable = parameter
@ PythonPP
class ChildClass ( ParentClass ):
def namespace ( public , private ):
@ staticinit
def StaticInit ():
ParentClass . staticinit ()
@ constructor
def Constructor ( parameter ):
ParentClass . constructor ( parameter )
public . publicVariable = "Public variable"
private . privateVariable = "Private variable"
@ method ( public )
def getPrivateVariable ():
return private . privateVariable
@ method ( public . static )
def getPrivateStaticVar ():
return private . static . privateStaticVar
@ special
def __str__ ():
return "ChildClass object" print ( ChildClass . publicStaticVar )
# > Private static variable
print ( ChildClass . getPrivateStaticVar ())
# > Private static variable
obj = ChildClass ( "Parameter value" )
print ( obj )
# > ChildClass object
print ( obj . publicVariable )
# > Public variable
print ( obj . getPrivateVariable ())
# > Parameter value
try :
obj . privateVariable # results in an error
except Exception as e :
print ( e )
# > 'ChildClass' object has no attribute 'privateVariable' 여기에서 Jupyter Notebook의 전체 예를 볼 수 있습니다.