Skip to main content

Python decorators to specify accessible scopes and enhance access control of methods.

Project description

Version SupportedVersions License

Installation

Built and tested on Python 3.12.
No requirements other than the module itself.

pip install python-access-modifiers

Introduction

python-access-modifiers is a lightweight Python package designed to enhance access control of methods within classes and modules.

Decorators

@private - Allows you to designate methods as "private", controlling the accessibility of the method based on the calling context.

Example Usage

Creating a private method inside of a class

from python_access_modifiers import private

class MyClass():
    @private
    def my_private_method(self) -> None:
        print("This is a private method")

    def my_public_method(self) -> None:
        print("This is a public method")
        self.my_private_method()

my_class = MyClass()
my_class.my_public_method()

Output

No exception is raised because my_private_method is called from within MyClass, not from outside of the accessible scope.

This is a public method
This is a private method

Calling a private method outside of the accessible scope

from python_access_modifiers import private

class MyClass():
    @private
    def my_private_method(self) -> None:
        print("This is a private method")

    def my_public_method(self) -> None:
        print("This is a public method")
        self.my_private_method()

my_class = MyClass()
my_class.my_private_method()

Output

RuntimeError: Cannot invoke private method "MyClass.my_private_method" from within the scope of "__main__"

Creating classes that inherit private methods

from python_access_modifiers import private

class Base():
    @private
    def my_private_method(self):
        print("This is a private method")

    def my_public_method(self):
        print("This is a public method")
        self.my_private_method()

class Child(Base):
    ...

child = Child()
child.my_public_method()

Output

my_private_method is called from within the Base class, therefore no exception is raised.

This is a public method
This is a private method

However, if my_private_method was called directly from outside of the accessible scope of the Base class, a RuntimeError would be raised.

child = Child()
child.my_private_method()

Output

RuntimeError: Cannot invoke private method "Base.my_private_method" from within the scope of "__main__"

Creating a private method from inside of a module

my_module.py

from python_access_modifiers import private

@private
def my_private_method():
    print("This method is private to my_module")

main.py

from my_module import my_private_method

my_private_method()

Output

RuntimeError: Cannot invoke private method "my_module.my_private_method" from module "__main__"

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

python_access_modifiers-1.1.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

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

python_access_modifiers-1.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file python_access_modifiers-1.1.0.tar.gz.

File metadata

  • Download URL: python_access_modifiers-1.1.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for python_access_modifiers-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e077f937e1eb24b2499d1c2872c19c9a0b8701e16bcfc110c594ceee241fdcd2
MD5 1605d21e160dbe522ed46fa59feebb3f
BLAKE2b-256 9233a049589b01e18bd8aad17f8ba5c109c02a8342691e250a687c4cfcc9ced3

See more details on using hashes here.

File details

Details for the file python_access_modifiers-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_access_modifiers-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d881b61e059ffacd82500450636d04671c74454cf81584b2e3304a92e3f8a63
MD5 45e91e8a5d0d9adaf0416a37560985f5
BLAKE2b-256 9341fc6e1b1376fffcbe11f75b47c938f9b1525f7bd85335af7e974bf03910ba

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