Allen Fowler wrote: > I seem to be having an issue with __getattr__() being called even if the > proporite already exists... I thought that this was not supposed to happen. > > Is there a typo somewhere, or I do i misunderstand things? > > class someclass(object): > > > def __init__(self, **kargs): > > self.valid_props = [ 'foo', 'bar', 'baz' ] > > for prop in self.valid_props: > if kargs.has_key(prop): > self.__setattr__(prop, kargs[prop]) > > def __getattr__(self,attr): > if attr in self.valid_props: > # This print should throw an exception, > # but it does not. It shows the value. > print "Oh no.. This was not found: %s" % self.__dict__[attr] > return 'n/a' > else: > raise AttributeError, attr
It works as I expect: In [28]: s=someclass(foo='FOO') In [29]: s.foo Out[29]: 'FOO' In [30]: s.bar ------------------------------------------------------------ Traceback (most recent call last): File "<ipython console>", line 1, in <module> File "<ipython console>", line 11, in __getattr__ <type 'exceptions.KeyError'>: 'bar' What did you try? What happened? What did you expect? Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor