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 - Defines a method as private, controlling the accessibility of the method based on the calling context. Private methods can only be invoked from inside of the same scope as the defining method.
@protected - Defines a method as protected, allowing the method to be invoked from the defining class and any classes that inherit from it.
@constant - Defines a method as constant, removing the ability for the method to be overriden. Note that this decorator can only be used within a class.
@field_access_modifiers - Allows a class to modify the accessibility of fields, defining them as public or protected.

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) -> None:
        print("This is a private method")

    def my_public_method(self) -> None:
        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() -> None:
    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__"

Creating a protected method

from python_access_modifiers import protected

class MyBaseClass():
    @protected
    def my_protected_method(self) -> None:
        print("This method is protected inside of MyBaseClass")

    def call_protected_method(self) -> None:
        self.my_protected_method()

class MySubClass(MyBaseClass):
    def call_protected_method_from_subclass(self) -> None:
        self.my_protected_method()
        
subclass = MySubClass()

subclass.call_protected_method()
subclass.call_protected_method_from_subclass()

Output

If my_protected_method is called from within MyBaseClass or from within one of it's subclasses (i.e: MySubClass), the method gets called normally.

This method is protected inside of MyBaseClass
This method is protected inside of MyBaseClass

However, if my_protected_method is called from outside of the accessible scope of the base class and it's subclasses, an exception will be raised.

subclass.my_protected_method()

Output

RuntimeError: Cannot invoke protected method "MyBaseClass.my_protected_method" from within the scope of "__main__"

Creating a constant method

Constant methods can only be created from inside of a class

from python_access_modifiers import constant

class MyClass():
    @constant
    def my_constant_method(self) -> None:
        print("This method is constant")

my_class = MyClass()
my_class.my_constant_method()

Output

This method is constant

Attempting to modify a constant method

def modified_method(self) -> None:
    print("This method has been modified")

my_class.my_constant_method = modified_method

Output

RuntimeError: "my_constant_method" is constant and cannot be overriden

Modifying the accessibility of fields

The accessibility of fields can only be modified from within classes. Currently, you can only create Protected fields. Fields are public by default.

Creating a protected field

from python_access_modifiers import field_access_modifiers, Protected

@field_access_modifiers
class MyClass():
    protected_field: Protected[int]
    
    def __init__(self, value: int) -> None:
        self.protected_field: int = value

    def show_protected_value(self) -> None:
        print(self.protected_field)

my_class = MyClass(7)
my_class.show_protected_value()

Output

7

Field access modifiers can be used with or without a type generic.

>>> field: Protected
>>> bool_field: Protected[bool]
>>> int_or_float_field: Protected[int | float]

Attempting to access a protected field

print(my_class.protected_field)

Output

RuntimeError: Cannot access protected field "MyClass.protected_field" from within "<module>"

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.3.1.tar.gz (5.3 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.3.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_access_modifiers-1.3.1.tar.gz
  • Upload date:
  • Size: 5.3 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.3.1.tar.gz
Algorithm Hash digest
SHA256 f6e5ced48ecd8f7031405e7d9de5e4f7a9ea64ddeb96d513d5768b567a26ec81
MD5 fc85e2aae07991e2d448700d0f28e942
BLAKE2b-256 fe4ed623fe50b1c595b31cdc638812dd718b94378307163dc9e2521491be5eeb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_access_modifiers-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee17155a0cf42a9d33470d899a65afa3d41e2955d8a71f7795224acfc5e85bca
MD5 d8d3c6df5c22eb1c5ffc45d25a7f099c
BLAKE2b-256 ba6e7b6cacc957c50d57179c243bb7144e58604fa11740ab2538bef40d1f75c2

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