Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26301 > unrolled thread
| Started by | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| First post | 2012-07-31 12:36 +0200 |
| Last post | 2012-08-01 12:59 -0400 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
NameError vs AttributeError Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-07-31 12:36 +0200
Re: NameError vs AttributeError Terry Reedy <tjreedy@udel.edu> - 2012-07-31 16:21 -0400
Re: NameError vs AttributeError Terry Reedy <tjreedy@udel.edu> - 2012-07-31 20:14 -0400
Re: NameError vs AttributeError Ethan Furman <ethan@stoneleaf.us> - 2012-08-01 08:53 -0700
Re: NameError vs AttributeError Terry Reedy <tjreedy@udel.edu> - 2012-08-01 12:59 -0400
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2012-07-31 12:36 +0200 |
| Subject | NameError vs AttributeError |
| Message-ID | <u1pke9-a46.ln1@satorlaser.homedns.org> |
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
[toc] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-07-31 16:21 -0400 |
| Message-ID | <mailman.2793.1343766099.4697.python-list@python.org> |
| In reply to | #26301 |
On 7/31/2012 6:36 AM, Ulrich Eckhardt wrote:
> 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.
You have observed a true fact. The two errors both involve
non-reconition of identifiers. In most cases, the identifiers are
looked-up in a dict.
> 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?
1. Why two separate exceptions:
a) operational: two separate exceptions are possible.
Name lookup and attribute lookup goes thru separate machinery. Name
lookup may go thru local, nonlocal, global (modular) and builtin
namespaces. Attribute lookup goes thru instance, class, and superclasses
to object. Name lookup is fixed except for nonlocal and global
declarations local assignments. Attribute lookup can be over-riden with
special methods (and special method lookup skips the instance).
b) practical: two separate exceptions are desirable.
One catches an exception to do something in the except clause. Name and
attribute errors often have different causes and different associated
actions.
def empty_and_process(poppable, process):
try:
pop = popable.pop
except AttributeError:
raise AttributeError("Can only empty_and_process objects with .pop
method")
Whoops, programming typo raises NameError. It should *not* be caught,
rather bug should be fixed.
Later usage error gets AttributeError, possibly documented. User decides
whether it represents a bug to be fixed (the default) or a control
signal to be caught and processed.
If one really does want the same action for the different mistakes,
"except (NameError, AttributeError):" is easy enough to write.
2. Why no special base exception (NameAttrError ?).
Python exceptions started as strings with NO hierarchy. The current
class system is still relatively flat. Flat is better than nested
because nesting introduces a new entities and new entities should not be
introduced without sufficient purpose and each one makes the whole
harder to learn and remember. I suspect that if you grepped a large
Python code corpus for this particular pair in except statements you
would find it rare.
---
Another example: KeyError and IndexError are both subscript errors, but
there is no SubscriptError superclass, even though both work thru the
same mechanism -- __getitem__. The reason is that there is no need for
one. In 'x[y]', x is usually intented to be either a sequence or
mapping, but not possibly both. In the rare cases when one wants to
catch both errors, one can easily enough. To continue the example above,
popping an empty list and empty set produce IndexError and KeyError
respectively:
try:
while True:
process(pop())
except (KeyError, IndexError):
pass # empty collection means we are done
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-07-31 20:14 -0400 |
| Message-ID | <mailman.2801.1343780060.4697.python-list@python.org> |
| In reply to | #26301 |
On 7/31/2012 4:49 PM, Chris Kaynor wrote: > On Tue, Jul 31, 2012 at 1:21 PM, Terry Reedy <tjreedy@udel.edu > <mailto:tjreedy@udel.edu>> wrote: > > Another example: KeyError and IndexError are both subscript errors, > but there is no SubscriptError superclass, even though both work > thru the same mechanism -- __getitem__. The reason is that there is > no need for one. In 'x[y]', x is usually intented to be either a > sequence or mapping, but not possibly both. In the rare cases when > one wants to catch both errors, one can easily enough. To continue > the example above, popping an empty list and empty set produce > IndexError and KeyError respectively: > > try: > while True: > process(pop()) > except (KeyError, IndexError): > pass # empty collection means we are done > > There is a base type for KeyError and IndexError: LookupError. > > http://docs.python.org/library/exceptions.html#exception-hierarchy Oh, so there is. Added in 1.5 strictly as a never-directly-raised base class for the above pair, now also directly raised in codecs.lookup. I have not decided if I want to replace the tuple in the code in my book. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-08-01 08:53 -0700 |
| Message-ID | <mailman.2836.1343838883.4697.python-list@python.org> |
| In reply to | #26301 |
Terry Reedy wrote: > On 7/31/2012 4:49 PM, Chris Kaynor wrote: >> On Tue, Jul 31, 2012 at 1:21 PM, Terry Reedy wrote: >>> Another example: KeyError and IndexError are both subscript errors, >>> but there is no SubscriptError superclass, even though both work >>> thru the same mechanism -- __getitem__. The reason is that there is >>> no need for one. In 'x[y]', x is usually intented to be either a >>> sequence or mapping, but not possibly both. In the rare cases when >>> one wants to catch both errors, one can easily enough. To continue >>> the example above, popping an empty list and empty set produce >>> IndexError and KeyError respectively: >>> >>> try: >>> while True: >>> process(pop()) >>> except (KeyError, IndexError): >>> pass # empty collection means we are done >>> >> There is a base type for KeyError and IndexError: LookupError. >> >> http://docs.python.org/library/exceptions.html#exception-hierarchy > > Oh, so there is. Added in 1.5 strictly as a never-directly-raised base > class for the above pair, now also directly raised in codecs.lookup. I > have not decided if I want to replace the tuple in the code in my book. I think I'd stick with the tuple -- LookupError could just as easily encompass NameError and AttributeError.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-08-01 12:59 -0400 |
| Message-ID | <mailman.2839.1343840406.4697.python-list@python.org> |
| In reply to | #26301 |
On 8/1/2012 11:53 AM, Ethan Furman wrote: > Terry Reedy wrote: >> On 7/31/2012 4:49 PM, Chris Kaynor wrote: >>> On Tue, Jul 31, 2012 at 1:21 PM, Terry Reedy wrote: >>>> one wants to catch both errors, one can easily enough. To continue >>>> the example above, popping an empty list and empty set produce >>>> IndexError and KeyError respectively: >>>> >>>> try: >>>> while True: >>>> process(pop()) >>>> except (KeyError, IndexError): >>>> pass # empty collection means we are done >>>> >>> There is a base type for KeyError and IndexError: LookupError. >>> >>> http://docs.python.org/library/exceptions.html#exception-hierarchy >> >> Oh, so there is. Added in 1.5 strictly as a never-directly-raised base >> class for the above pair, now also directly raised in codecs.lookup. I >> have not decided if I want to replace the tuple in the code in my book. > > I think I'd stick with the tuple -- LookupError could just as easily > encompass NameError and AttributeError. Thank you. Having to remember exactly which lookup error is encompassed by LookupError illustrates my point about the cost of adding entities without necessity. It also illustrates the importance of carefull naming. SubscriptError might have been better. -- Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web