Skip to main content

Python 3, but with proper encapsulation.

Project description

PythonPP

Python 3, but with proper encapsulation.

View on PyPI

PythonPP stands for Python Plus Plus.

Contributors

Ronak Badhe / Kento Nishi

Installation

The package is available on PyPI. You can install the package with the following command:

pip install pythonpp

Usage

A detailed example of a class utilizing PythonPP is available in this Jupyter notebook.

A PythonPP object must extend PythonPP's objpp class. The constructor must also call the __init__ method of the parent objpp class. When no __init method is defined, super().__init__() will be executed automatically.

Example:

from pythonpp import *

# Class extends pythonpp's objpp class.
class Test(objpp):
    # Class constructor
    def __init__(self):
        # Call objpp's constructor.
        super().__init__()
    def namespace(public, private):
        # public: the public scope.
        # private: the private scope.
        pass

Alternatively, you can create your class without using a wildcard import.

import pythonpp as ppp

class Test(ppp.objpp):
    def __init__(self):
        super().__init__()
    def namespace(public, private):
        pass

All variable and method declarations must be done in the namespace method.

The namespace method has two parameters.

Parameter Value
public The public scope. All variables in this scope can be accessed externally.
private The private scope. All variables in this scope can only be accessed internally.

You can define public and private variables using these scopes.

All variables and methods are declared private by default.

Example:

# public variable
public.hello = "hello"

# private variable
private.world = "world"

You can also use the public and private scopes to declare methods.

Example:

# public method
@method(public)
def publicMethod():
    print("Called publicMethod")

# private method
@method(private)
def privateMethod():
    print("Called privateMethod")

Public variables and methods can be accessed as usual.

Example:

test = Test()
# works as normal
print(test.hello)
# also works as normal
test.publicMethod()

However, private variables and methods cannot be accessed externally.

Example:

test = Test()
# results in an error
print(test.world)
# also results in an error
test.privateMethod()

Example Class

View the full Jupyter notebook here.

from pythonpp import *

class Test(objpp):
    # Class constructor
    def __init__(self):
        # Call objpp's constructor.
        super().__init__()

    # Place all methods and field declerations here.
    def namespace(public, private):
        # public variable
        public.hello = "hello"
        # private variable
        private.world = "world"

        # public method
        @method(public)
        def publicMethod():
            print("Called publicMethod")

        # private method
        @method(private)
        def privateMethod():
            print("Called privateMethod")

        # public method to call the private method.
        @method(public)
        def callPrivateMethod():
            print("Calling privateMethod()")
            # Call the private method
            private.privateMethod()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythonpp-1.1.tar.gz (3.1 kB view hashes)

Uploaded Source

Built Distribution

pythonpp-1.1-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page