Package: pythonpp
Python++A robust Java-style OOP system for Python, with support for statics, encapsulation, and inheritance.View on PyPI/
Built byKento NishiandRonak BadhePython++ allows Python programmers to use object oriented programming principles in Python.InstallationThe package is available on PyPI.
You can install the package with the following command:pipinstallpythonppUsageImporting the LibraryYou can import Python++ with the following:frompythonppimport*Class DeclarationDeclare Python++ classes with the@PythonPPdecorator.@PythonPPclassMyClass:pass# class code hereNamespace and ScopesDeclare variables and methods for Python++ classes withinnamespace.@PythonPPclassMyClass:defnamespace(public,private):pass# methods and variables hereCode withinnamespacehas access to the following scopes:ScopeDescriptionpublicThe public instance scope.privateThe private instance scope.public.staticThe public static scope.private.staticThe private static scope.Static InitializersDeclare static initializers for Python++ classes using the@staticinitdecorator.
Static initializers do not have access to instance variables and methods.
Static initializers cannot have input parameters.@PythonPPclassMyClass:defnamespace(public,private):@staticinitdefStaticInit():public.static.publicStaticVar="Static variable (public)"private.static.privateStaticVar="Static variable (private)"Alternatively, static variables can be declared in the barenamespaceif the variable assignments are constant. Using bare static variable declarations arenot recommended.ConstructorsConstructors can be declared using the@constructordecorator. Constructors can have input parameters.@PythonPPclassMyClass:defnamespace(public,private):@constructordefConstructor(someValue):public.publicInstanceVar="Instance variable (public)"public.userDefinedValue=someValueMethod DeclarationsMethods are declared using the@method(scope)decorator with thepublicandprivatescopes innamespace.@PythonPPclassMyClass:defnamespace(public,private):@method(public)defpublicMethod():pass# public instance method here@method(private)defprivateMethod():pass# private instance method here@method(public.static)defpublicStaticMethod():pass# public static method here@method(private.static)defprivateStaticMethod():pass# private static method hereSpecial MethodsDeclare special built-in methods using the@specialdecorator.@PythonPPclassMyClass:defnamespace(public,private):@specialdef__str__():return"Some string value"InheritanceClasses can extend other classes using standard Python class inheritance.@PythonPPclassParentClass:defnamespace(public,private):@staticinitdefStaticInit():public.static.staticVar="Static variable"@constructordefConstructor(param):print("Parent constructor")public.param=param@PythonPPclassChildClass(ParentClass):# ChildClass extends ParentClassdefnamespace(public,private):@staticinitdefStaticInit():ParentClass.staticinit()# Call parent static initializerpublic.static.staticVar2="Static variable 2"@constructordefConstructor(param):# Call parent constructorParentClass.constructor(param)Quickstart Examplefrompythonppimport*@PythonPPclassParentClass:defnamespace(public,private):@staticinitdefStaticInit():public.static.publicStaticVar="Public static variable"private.static.privateStaticVar="Private static variable"@constructordefConstructor(parameter):private.privateVariable=parameter@PythonPPclassChildClass(ParentClass):defnamespace(public,private):@staticinitdefStaticInit():ParentClass.staticinit()@constructordefConstructor(parameter):ParentClass.constructor(parameter)public.publicVariable="Public variable"private.privateVariable="Private variable"@method(public)defgetPrivateVariable():returnprivate.privateVariable@method(public.static)defgetPrivateStaticVar():returnprivate.static.privateStaticVar@specialdef__str__():return"ChildClass object"print(ChildClass.publicStaticVar)# > Private static variableprint(ChildClass.getPrivateStaticVar())# > Private static variableobj=ChildClass("Parameter value")print(obj)# > ChildClass objectprint(obj.publicVariable)# > Public variableprint(obj.getPrivateVariable())# > Parameter valuetry:obj.privateVariable# results in an errorexceptExceptionase:print(e)# > 'ChildClass' object has no attribute 'privateVariable'Full ExampleYou can view the full example Jupyter notebookhere.