Created
February 3, 2020 15:58
-
-
Save RafaelPalomar/c61922d1015dcc04a2dc4a23739fd937 to your computer and use it in GitHub Desktop.
Small snippet to use setters/gettes with dictionary-like variable acess #Python #Setters #Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A(): | |
def __init__(self, attr1): | |
self.attr1 = attr1 | |
def __getitem__(self, key): | |
print('__getitem__ called') | |
return getattr(self, key) | |
def __setitem__(self, key, value): | |
print('__setitem__ called') | |
setattr(self, key, value) | |
@property | |
def attr1(self): | |
print('attr1 property getter called') | |
return self.__attr1 | |
@attr1.setter | |
def attr1(self, value): | |
print('attr1 property setter called') | |
self.__attr1 = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment