[Tutor] Keeping a list of attributes of a certain type
Kent Johnson
kent37 at tds.net
Thu Jan 14 12:43:09 CET 2010
On Wed, Jan 13, 2010 at 11:15 PM, Guilherme P. de Freitas
<guilherme at gpfreitas.com> wrote:
> Ok, I got something that seems to work for me. Any comments are welcome.
>
>
> class Member(object):
> def __init__(self):
> pass
>
>
> class Body(object):
> def __init__(self):
> self.members = []
>
> def __setattr__(self, obj, value):
> if isinstance(value, Member):
> self.members.append(obj)
> object.__setattr__(self, obj, value)
> else:
> object.__setattr__(self, obj, value)
That's fine but there is no need to duplicate the object.__setattr__() call:
def __setattr__(self, obj, value):
if isinstance(value, Member):
self.members.append(obj)
object.__setattr__(self, obj, value)
> def __delattr__(self, obj):
> if isinstance(getattr(self, obj), Member):
> self.members.remove(obj)
> object.__delattr__(self, obj)
> else:
> object.__delattr__(self, obj)
Same here.
Kent
More information about the Tutor
mailing list