Package: python-access-modifiers
InstallationBuilt and tested on Python 3.12.No requirements other than the module itself.pip install python-access-modifiersIntroductionpython-access-modifiersis 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 UsageCreating a private method inside of a classfrompython_access_modifiersimportprivateclassMyClass():@privatedefmy_private_method(self)->None:print("This is a private method")defmy_public_method(self)->None:print("This is a public method")self.my_private_method()my_class=MyClass()my_class.my_public_method()OutputNo exception is raised becausemy_private_methodis called from withinMyClass, not from outside of the accessible scope.This is a public method
This is a private methodCalling a private method outside of the accessible scopefrompython_access_modifiersimportprivateclassMyClass():@privatedefmy_private_method(self)->None:print("This is a private method")defmy_public_method(self)->None:print("This is a public method")self.my_private_method()my_class=MyClass()my_class.my_private_method()OutputRuntimeError: Cannot invoke private method "MyClass.my_private_method" from within the scope of "__main__"Creating classes that inherit private methodsfrompython_access_modifiersimportprivateclassBase():@privatedefmy_private_method(self)->None:print("This is a private method")defmy_public_method(self)->None:print("This is a public method")self.my_private_method()classChild(Base):...child=Child()child.my_public_method()Outputmy_private_methodis called from within theBaseclass, therefore no exception is raised.This is a public method
This is a private methodHowever, ifmy_private_methodwas called directly from outside of the accessible scope of theBaseclass, aRuntimeErrorwould be raised.child=Child()child.my_private_method()OutputRuntimeError: Cannot invoke private method "Base.my_private_method" from within the scope of "__main__"Creating a private method from inside of a modulemy_module.pyfrompython_access_modifiersimportprivate@privatedefmy_private_method()->None:print("This method is private to my_module")main.pyfrommy_moduleimportmy_private_methodmy_private_method()OutputRuntimeError: Cannot invoke private method "my_module.my_private_method" from module "__main__"Creating a protected methodfrompython_access_modifiersimportprotectedclassMyBaseClass():@protecteddefmy_protected_method(self)->None:print("This method is protected inside of MyBaseClass")defcall_protected_method(self)->None:self.my_protected_method()classMySubClass(MyBaseClass):defcall_protected_method_from_subclass(self)->None:self.my_protected_method()subclass=MySubClass()subclass.call_protected_method()subclass.call_protected_method_from_subclass()OutputIfmy_protected_methodis called from withinMyBaseClassor 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 MyBaseClassHowever, ifmy_protected_methodis called from outside of the accessible scope of the base class and it's subclasses, an exception will be raised.subclass.my_protected_method()OutputRuntimeError: Cannot invoke protected method "MyBaseClass.my_protected_method" from within the scope of "__main__"Creating a constant methodConstant methods can only be created from inside of a classfrompython_access_modifiersimportconstantclassMyClass():@constantdefmy_constant_method(self)->None:print("This method is constant")my_class=MyClass()my_class.my_constant_method()OutputThis method is constantAttempting to modify a constant methoddefmodified_method(self)->None:print("This method has been modified")my_class.my_constant_method=modified_methodOutputRuntimeError: "my_constant_method" is constant and cannot be overridenModifying the accessibility of fieldsThe accessibility of fields can only be modified from within classes. Currently, you can only createProtectedfields. Fields are public by default.Creating a protected fieldfrompython_access_modifiersimportfield_access_modifiers,Protected@field_access_modifiersclassMyClass():protected_field:Protected[int]def__init__(self,value:int)->None:self.protected_field:int=valuedefshow_protected_value(self)->None:print(self.protected_field)my_class=MyClass(7)my_class.show_protected_value()Output7Field 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 fieldprint(my_class.protected_field)OutputRuntimeError: Cannot access protected field "MyClass.protected_field" from within "<module>"