NameError vs AttributeError

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Tue Jul 31 06:36:14 EDT 2012


Hi!

Using Python 2.7, I stumbled across the fact that 'self.xy' raises an 
AttributeError if self doesn't have an 'xy' as attribute, but 'xy' will 
instead raise a NameError. To some extent, these two are very similar, 
namely that the name 'xy' couldn't be resolved in a certain context, but 
they don't have a common baseclass.

I guess one of the reasons is behind the way that Python handles 
variable lookup, the plain 'xy' will find local and global names while 
'self.xy' will only look into onself. However, this vague idea is far 
from enough to explain it to someone else.

Can someone help me out?

Uli



More information about the Python-list mailing list