NameError vs AttributeError

Ulrich Eckhardt ulrich.eckhardt at
Tue Jul 31 12:36:14 CEST 2012


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?


More information about the Python-list mailing list