Skip to content

Instantly share code, notes, and snippets.

@RafaelPalomar
Created February 3, 2020 15:58
Show Gist options
  • Save RafaelPalomar/c61922d1015dcc04a2dc4a23739fd937 to your computer and use it in GitHub Desktop.
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
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