Python decorators to specify accessible scopes and enhance access control of methods.
Project description
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.
@protected - Allows for the creation of "protected" methods, allowing access from only within the class where the method is defined, or within one of it's subclasses.
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__"
Creating a protected method
from python_access_modifiers import protected
class MyBaseClass():
@protected
def my_protected_method(self):
print("This method is protected inside of MyBaseClass")
def call_protected_method(self):
self.my_protected_method()
class MySubClass(MyBaseClass):
def call_protected_method_from_subclass(self):
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 as normal.
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 base class and outside of the base class'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__"
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_access_modifiers-1.2.0.tar.gz.
File metadata
- Download URL: python_access_modifiers-1.2.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
210ddc78347c69e90f186d48557c940bb711dd20dbdd24b1d23a7bb3bccf614d
|
|
| MD5 |
e4e2e53ec78eb88411a224bbda91af24
|
|
| BLAKE2b-256 |
50d4c9c8f2c238da57faf0da91edf493d3b2f8ffd691993ed329d82f2bf833cd
|
File details
Details for the file python_access_modifiers-1.2.0-py3-none-any.whl.
File metadata
- Download URL: python_access_modifiers-1.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70a35df2faa8a533bee81780d0f6cedc6be25226278e4be0709382eae12aa6e3
|
|
| MD5 |
ddb7288baf95995a6fabdd1d4ab62dec
|
|
| BLAKE2b-256 |
c73a1ff65411e65184f1e3e3353f66b00cc103d1fa40b6f8631053db6718a4cf
|