This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author veky
Recipients barry, levkivskyi, martin.panter, mbussonn, r.david.murray, rhettinger, serhiy.storchaka, veky
Date 2017-08-17.04:47:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1502945270.21.0.653872406003.issue29593@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-08-17 04:47:50vekysetrecipients: + veky, barry, rhettinger, r.david.murray, martin.panter, serhiy.storchaka, levkivskyi, mbussonn
2017-08-17 04:47:50vekysetmessageid: <1502945270.21.0.653872406003.issue29593@psf.upfronthosting.co.za>
2017-08-17 04:47:50vekylinkissue29593 messages
2017-08-17 04:47:48vekycreate