Skip to content

Instantly share code, notes, and snippets.

@hidsh
Created February 28, 2012 08:12
Show Gist options
  • Save hidsh/1930548 to your computer and use it in GitHub Desktop.
Save hidsh/1930548 to your computer and use it in GitHub Desktop.
python test __setattr_()
# 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