Vedran Čačić added the comment:

Re: http://bugs.python.org/msg288032

I don't know what example Raymond had, but your example

In [5]: def foo():
   ...:     n = 1 
   ...:     def g(): # imagine a more complex g
   ...:         del n 
   ...:     g()
   ...:     print(n) #UnboundLocalError

is completely wrong, at least in what you imply in the comments. It would be a 
horrible language where functions could just delete nonlocal names in the same 
way as local ones. (You can force it by adding `nonlocal n` inside g. But 
"nonlocal keyword is ugly for a good reason", to quote Guido.:)

It's interesting that in your example, you also get an ULE, but for a whole 
different reason: it is _g_'s call that tries to _delete_ (a kind of 
"referencing") a local (to _it_) variable n, that hasn't yet been set _in the 
scope of g_. You can see it easily in the traceback, and you'd probably see it 
if you didn't have preconceived notions about what happens. :-]

You can easily see it by removing print(n), it is a red herring, or even more 
simply, just define g globally:

    >>> def g(): del n
    >>> g()
    UnboundLocalError: ...

You'll get the same phenomenon.

---

About the error message: I agree with Serhiy. See 
http://www.drmaciver.com/2013/07/a-case-study-in-bad-error-messages/#comment-9095
 (and the whole discussion if you miss the context). Trying to cover all the 
possible situations where an ULE might get raised is (a) futile, (b) worsening 
things, since experienced programmers already know that error messages are just 
approximations, and beginners are best helped with just the most common case.

May I ask why do you even teach "del on simple names" (as opposed to del on 
list items, which is kinda useful since it has a sideeffect) to beginners? It's 
very rarely needed, especially in a language with a powerful garbage collector.

----------
nosy: +veky

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29593>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to