add getattr/setattr test
This commit is contained in:
parent
3bd8bfa367
commit
7387443a80
1 changed files with 16 additions and 0 deletions
16
attributes.py
Normal file
16
attributes.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
from __future__ import division, print_function
|
||||||
|
|
||||||
|
class SomeClass(object):
|
||||||
|
def __setattr__(self, name, value):
|
||||||
|
print("Setting {} to value {}".format(name, value))
|
||||||
|
self.__dict__[name] = value
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
print("Getting {}".format(name))
|
||||||
|
return None
|
||||||
|
|
||||||
|
o = SomeClass()
|
||||||
|
o.xxx = "yyy"
|
||||||
|
o.xxx = "zzz"
|
||||||
|
print(o.xxx) # Note: __getattr__ does not get called here!
|
||||||
|
print(o.nope)
|
Loading…
Add table
Add a link
Reference in a new issue