[Tutor] __getattr__(): Is this right?

Allen Fowler allen.fowler at yahoo.com
Tue Oct 16 02:33:34 CEST 2007


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



       
---------------------------------
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071015/fb8490fd/attachment-0001.htm 


More information about the Tutor mailing list