A robust Java-style OOP system for Python, with support for statics, encapsulation, and inheritance.
View on PyPI / Built by Kento Nishi and Ronak Badhe
Python++ allows Python programmers to use object oriented programming principles in Python.
The package is available on PyPI. You can install the package with the following command:
pip install pythonpp
You can import Python++ using a wildcard import statement.
from pythonpp import *
Declare Python++ classes with the @PythonPP
decorator.
@PythonPP
class MyClass:
pass # class code here
Declare variables and methods for Python++ classes within namespace
.
@PythonPP
class MyClass:
def namespace(public, private):
pass # methods and variables here
Code within namespace
has access to the following scopes:
Scope | Description |
---|---|
public |
The public instance scope. |
private |
The private instance scope. |
public.static |
The public static scope. |
private.static |
The private static scope. |
Declare static initializers for Python++ classes using the @staticinit
decorator.
Static initializers do not have access to instance variables and methods.
Static initializers cannot have input parameters.
@PythonPP
class MyClass:
def namespace(public, private):
@staticinit
def StaticInit():
public.static.publicStaticVar = "Static variable (public)"
private.static.privateStaticVar = "Static variable (private)"
Alternatively, static variables can be declared in the bare namespace
if the variable assignments are constant. Using bare static variable declarations are not recommended.
Constructors can be declared using the @constructor
decorator. Constructors can have input parameters.
@PythonPP
class MyClass:
def namespace(public, private):
@constructor
def Constructor(someValue):
public.publicInstanceVar = "Instance variable (public)"
public.userDefinedValue = someValue
Methods are declared using the @method(scope)
decorator with the public
and private
scopes in namespace
.
@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
Declare special built-in methods using the @special
decorator.
@PythonPP
class MyClass:
def namespace(public, private):
@special
def __str__():
return "Some string value"
Classes can extend other classes using standard Python class inheritance.
@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'
You can view the full example Jupyter notebook here.