Created
February 28, 2012 08:12
-
-
Save hidsh/1930548 to your computer and use it in GitHub Desktop.
python test __setattr_()
This file contains hidden or 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
# test: set of class object | |
class part(object): | |
def __init__(self, name='unknown', val=100, flag=False): | |
self.name = name | |
self.val = val | |
self.flag = flag | |
def __setattr__(self, name, value): | |
if name=='name': | |
# if isinstance(value, str): self.__dict__[name] = value | |
if isinstance(value, str): object.__setattr__(self, name, value) | |
elif name=='flag': | |
# if isinstance(value, bool): self.__dict__[name] = value | |
if isinstance(value, bool): object.__setattr__(self, name, value) | |
else: | |
# self.__dict__[name] = value | |
object.__setattr__(self, name, value) | |
# test | |
p = part() | |
p.name = None | |
p.val = 'aa' | |
p.flag = None | |
print p.__dict__ | |
p.name = 'a' | |
p.val = 2 | |
p.flag = True | |
print p.__dict__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment