Module to increase encapsulation in Python, not allowing the access to private members outside their classes
Use the package manager pip to install PrivateAttributesProject.
pip3 install PrivateAttributesDecoratorfromPrivateAttributesDecoratorimportprivate_attributes_decorator# We add the attributes we want to make private, also optionally we can add the keyword# parameter allow_deep_copy and set it to True in case we want to allow our class to create# deep copies of its objects. By default, it is set to False@private_attributes_decorator.private_attributes_dec("a")classExample:
def__init__(self):
self.a: int=12self.b: int=12self.c: int=12self.__d: int=12self.__e: int=12e: Example=Example()
# Case 1.1: Trying to access the private attribute outside the class -> We will raise an AttributeErrortry:
print(e._Example__d)
exceptAttributeError:
assertTrueprint("The attribute __d (_Example__d) can't be accessed outside the class")
else:
assertFalse# Case 2.1: We try to access a public attribute that we altered to make private using the arguments in the decorator# -> We raise an AttributeErrortry:
print(e.a)
exceptAttributeError:
assertTrueprint("The attribute a can't be accessed outside the class")
else:
assertFalse# Case 6: We try to do a deepcopy of an object, but we set some parameters as private in its class and did not# set the attribute allow_deep_copy to True@private_attributes_decorator.private_attributes_dec("arg1")classNewExample:
def__init__(self,arg1: int, arg2: int, arg3: int):
self.arg1: int=arg1self__arg2: int=arg2self.arg3: int=arg3importcopyne: NewExample=NewExample(1,2,3)
# Case 6.1: We try to do a deepcopy of an object, but we set some parameters as private in its class and did not# set the attribute allow_deep_copy to True -> deepcopy raises a copy.Error Exceptiontry:
nec=copy.deepcopy(ne)
exceptcopy.Error:
print("We did not allow for deep copies so the method raised a copy.Error Exception")
assertTrueelse:
print("We did not allow for deep copies, but the deep copy was produced. Something is not right here")
assertFalse# Case 7: We allowed deep copies in our class and we try to do a deepcopy of an object, we set the some parameters# as private in its class@private_attributes_decorator.private_attributes_dec("arg1",allow_deep_copy=True)classNewExampleAllowDeepCopy:
def__init__(self,arg1: int, arg2: int, arg3: int):
self.arg1: int=arg1self__arg2: int=arg2self.arg3: int=arg3ned: NewExampleAllowDeepCopy=NewExampleAllowDeepCopy(1, 2, 3)
# Case 7.1: We allowed deep copies in our class and we try to do a deepcopy of an object, we set the some parameters# as private in its class. We try to create a deepcopy of the object -> The copy is createdtry:
nedc=copy.deepcopy(ned)
exceptcopy.Error:
print("We allowed for deep copies but the method raised a copy.Error Exception, something is wrong")
assertFalseelse:
print("The copy was created without issues")
print(nedc)
assertTrue# For more examples and information check the tests in the source fileFor major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.