[Tutor] question about descriptors

Albert-Jan Roskam sjeik_appie at hotmail.com
Fri Nov 13 06:44:47 EST 2015


> To: tutor at python.org
> From: __peter__ at web.de
> Date: Fri, 13 Nov 2015 09:26:55 +0100
> Subject: Re: [Tutor] question about descriptors
> 
> Albert-Jan Roskam wrote:
> 
> >> __getattr__() is only invoked as a fallback when the normal attribute
> >> lookup fails:
> > 
> > 
> > Aha.. and "normal attributes" live in self.__dict__?
> 
> I meant "normal (attribute lookup)" rather than "(normal attribute) lookup".
> __getattr__() works the same (I think) when there is no __dict__: 
> 
> >>> class A(object):
> ...     __slots__ = ["foo"]
> ...     def __getattr__(self, name):
> ...             print "looking for", name
> ...             return 42
> ... 
> >>> a = A()
> >>> a.foo
> looking for foo
> 42
> >>> a.__dict__
> looking for __dict__
> 42
> >>> a.foo = "bar"
> >>> a.foo
> 'bar'

Thank you again for the explanation. Much appreciated. I had not even thought about __slots__ yet.

 		 	   		  


More information about the Tutor mailing list