Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Access modifiers for Python

Build Status PyPI

This package provides two access modifiers for Python: private methods and protected methods. The goal is to be able to document methods as being private or protected and to provide basic guards against accidentally calling private and protected methods from outside the allowed scopes.

How to use

Example usage of private methods:

from access_modifiers import privatemethod

class Class:
    @privatemethod
    def private_method(self) -> str:
        return "private method"

    def public_method(self) -> str:
        return "public method calls " + self.private_method()

c = Class()
print(c.public_method())  # Prints "public method calls private method"
print(c.private_method())  # Raises an exception

Example usage of protected methods:

from access_modifiers import protectedmethod

class Class:
    @protectedmethod
    def protected_method(self) -> str:
        return "protected method"

    def public_method(self) -> str:
        return "public method calls " + self.protected_method()


class Subclass(Class):
    @protectedmethod
    def protected_method(self) -> str:
        return "overridden protected method calls " + super().protected_method()

c = Subclass()
print(c.public_method())  # Prints "public method calls overridden protected method calls protected method"
print(c.protected_method())  # Raises an exception

Private methods can be combined with static methods. Note that the order matters: staticmethod should be the outermost decorator.

from access_modifiers import privatemethod

class Class:
    @staticmethod
    @privatemethod
    def static_private_method() -> str:
        return "static private method"

    def public_method(self) -> str:
        return "public method calls " + self.static_private_method()

c = Class()
print(c.public_method())  # Prints "public method calls static private method"
print(c.static_private_method())  # Raises an exception

Combining protected methods with static methods is not supported. Combining access modifiers with class methods is not supported (yet).

Performance

The access modifier decorators work by looking at the code that is calling the decorator to decide whether it is allowed to call the method. To do so, the decorators use implementation details of CPython, like sys._getframe() and the names of code objects such as lambdas and modules. These checks are done on each method call. Consequently, there is a considerable performance impact. Therefore it's recommended to use the access modifiers during testing and turn them off in production using the access_modifiers.disable() method. Note that you need to call this method before any of the access modifier decorators are evaluated, i.e.:

from access_modifiers import disable, privatemethod

disable()  # This will disable the access checks

class Class:
    @privatemethod
    def private_method(self) -> str:
        return "private_method"

disable()  # Calling disable here will not work, Class.private_method has already been wrapped

Installation

The package is available from the Python Package Index, install with pip install access-modifiers.

Development

To clone the repository: git clone git@github.com:fniessink/access-modifiers.git.

To install the development dependencies: pip install -r requirements-dev.txt.

To run the unittests and measure the coverage (which should always be at 100%): ci/unittest.sh.

To run Pylint (which should score a 10) and Mypy (which shouldn't complain): ci/quality.sh.

The implementation is driven by (unit) tests and has 100% unit test statement and branch coverage. Please look at the tests to see which usage scenario's are currently covered.

Release files for access-modifiers 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for access-modifiers 0.3.1
File Size Uploaded
access_modifiers-0.3.1.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for access-modifiers 0.3.1
File Interpreter ABI Platform
access_modifiers-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / access_modifiers-0.3.1.tar.gz

Download URL access_modifiers-0.3.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4e5cbd38b48f2c0a5835f4b55b2ccf837a7ddacf41a590e9ea3a7cf9ca73b663
BLAKE2b-256 checksum
How to use checksums
5e94187bf6ab3c0e474b3bbf30d886ff9b10d13601209c70566773305e7f075c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1

Release files / access_modifiers-0.3.1-py3-none-any.whl

Download URL access_modifiers-0.3.1-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02f5cee19501173708e014ce4f4df89ee49641836aac4543cee6a7d7478e3f87
BLAKE2b-256 checksum
How to use checksums
6682ed8df97f3a3099ef55d22bad4119aa64526f3538d6ca8a5147cf151ee296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.1
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page