class fields vs __getattr__

Joshua Marshall jmarshal at mathworks.com
Thu Mar 8 19:32:21 EST 2001


If you try to get a field of an object, it seems __getattr__ is called
only if no such field exists on the class.  For exmaple:

  >>> class T:
  ...   x = 1
  ...   def __getattr__(self, name):
  ...     return 2
  ... 
  >>> ob = T() 
  >>> ob.foo
  2
  >>> ob.x
  1

I would have expected ob.x to return 2.  Can anyone explain the
rationale behind this behavior?



More information about the Python-list mailing list