Skip to main content

Access modifiers for python

Project description

protecc

Access modifiers for python.

Setup

PIP install

pip install protecc

From the source

git clone https://github.com/avinashshenoy97/protecc
cd protecc
python setup.py install

Usage

Old-Style Classes

Inherit from/extend the protecc class.

from protecc import protecc

class regulatedClass(protecc):
    def __init__(self):
        self.publicVariable = 'public value'
        self._privateVariable = 'private value'

    def publicMethod(self):
        return True

    def __privateMethod(self):
        return True

    def getPrivateVariable(self):
        return self._privateVariable

    def privateMethodProxy(self):
        return self.__privateMethod()

New-Style Classes (type)

If you prefer metaclass (for better readability/what not), use metaProtecc.

class metaRegulatedClass(metaclass=metaProtecc):
    def __init__(self):
        self.publicVariable = 'public value'
        self._privateVariable = 'private value'

    def publicMethod(self):
        return True

    def __privateMethod(self):
        return True

    def getPrivateVariable(self):
        return self._privateVariable

    def privateMethodProxy(self):
        return self.__privateMethod()

When accessing "private" methods (i.e, methods that begin with either a single or two underscores), this exception is raised

>>> r = regulatedClass()
>>> r.__privateMethod()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/avinashshenoy/STUFF/projects/protecc/protecc.py", line 23, in __protecced_getattribute__
    raise AccessException('Cannot access ' + varType + ' member ' + name) from None
protecc.AccessException: Cannot access private member __privateMethod

Private methods can still be accessed from within other class methods, as is expected:

>>> r = regulatedClass()
>>> r.privateMethodProxy()
True

Note

  • This is, by no means, fool-proof access protection.
  • "Private members" are those members whose names are mangled by Python, as per PEP 8 conventions, i.e, starting with 2 underscores and having not more than one trailing underscore.
  • Additionally, "protected members" are those members whose names are preceded by a single underscores. These members cannot be accessed from outside the class as well, as is expected.

Primary Contributors

Avinash Shenoy

License

This project is released under the MIT License.

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

protecc-0.0.2.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

protecc-0.0.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file protecc-0.0.2.tar.gz.

File metadata

  • Download URL: protecc-0.0.2.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for protecc-0.0.2.tar.gz
Algorithm Hash digest
SHA256 716d255fb998f27d952a3ce733ba368e299c78cd5df6802a9125c46c940833b3
MD5 2db369cc7ced746615e76e53de4b6bb1
BLAKE2b-256 e020b3a4824bb042b8e128f06318ddf59dd76776fd9d655a4e5e0390e250edba

See more details on using hashes here.

File details

Details for the file protecc-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: protecc-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for protecc-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e5aee5758d40f96d4114c2056b4bcea0137dfc648f1c8b8d578e99683557157f
MD5 3a14b0ae31293c917f00369634bcb6f2
BLAKE2b-256 530f82e55166c13b3ec959ba23ac3dd5dd7436510333d9702a63b7ddf0005657

See more details on using hashes here.

Supported by

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