ActiveState Code

Recipe 497009: "safely" defining __setattr__


Overriding __setattr__ in classes requires care when setting attributes yourself. Here's an idea for safely setting attributes in __init__.

Update: this idea doesn't work. See Mike Foord's recipe for one that does:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class MyClass(object):
  def __init__(self, foo, bar):
    # set attributes normally here
    self.foo = foo
    self.bar = bar

    # override __setattr__
    # NOTE: doesn't really work, __setattr_impl won't be called.
    self.__setattr__ = self.__setattr_impl

  def __setattr_impl(self, name, value):
    pass # definition goes here

Discussion

For different approach to the same issue, see Mike Foord's recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916

Comments

  1. 1. At 2:51 p.m. on 31 aug 2006, tomer filiba said:

    doesn't work :(. __setattr__ will only work if it exists at the class level.

    here's a snapshot:

    >>> class MyClass(object):
    ...   def __init__(self, foo, bar):
    ...     # set attributes normally here
    ...     self.foo = foo
    ...     self.bar = bar
    ...
    ...     # override __setattr__
    ...     self.__setattr__ = self.__setattr_impl
    ...
    ...   def __setattr_impl(self, name, value):
    ...     print "blah"
    ...
    >>>
    >>> m=MyClass(1,2)
    >>> m.x=8
    >>>
    

    "blah" isn't printed...

    (sup dude? i'm bored, i know... i'm telling you, down with types and classes. we don't need them. go antitype! :)

  2. 2. At 4:06 p.m. on 31 aug 2006, Ian Bicking said:

    new-style. There's a good chance this would work with old-style classes (haven't tested though). New-style classes are more picky about magic methods being in the class.

  3. 3. At 1:06 a.m. on 1 sep 2006, Ori Peleg (the author) said:

    My mistake. This doesn't seem to work in new-style _or_ old-style classes. I don't know why I thought it did work.

Sign in to comment