Easy questions from a python beginner

Alf P. Steinbach /Usenet alf.p.steinbach+usenet at gmail.com
Mon Jul 12 18:55:59 EDT 2010


* Rami Chowdhury, on 13.07.2010 00:14:
> Perhaps I'm misunderstanding, but ...
>
> On Jul 12, 2010, at 13:57 , Alf P. Steinbach /Usenet wrote:
>>
>> Existence of a variable means, among other things, that
>>
>>   * You can use the value, with guaranteed effect (either unassigned exception
>>     or you get a proper value)
>
> Surely by that definition any variable in any Python program "exists" -- you
> are guaranteed to get one of NameError, UnboundLocalError, or a value. That
> seems to argue away the meaning of the word entirely, and renders it not
> particularly useful.

No, you're conflating non-existence (NameError) with accessing the value of an 
existing but unassigned variable (UnboundLocalError). It is like arguing that 
vacuum is the same as cement because sticking your head into it for ten minutes 
or so yields an effect  --  you're dead  --  that in many ways is just about the 
same. However, the tangible existence of cement means that one can pour it over 
your head, /adding/ the cement, while it's difficult to pour vacuum over your 
head; rather, for the latter effect one would need to /remove/ the air around 
your head. OK, the analogy halts a little. But I think you'll get it.

Similarly, you can add a variable and thereby cause am UnboundLocalError, and 
you can remove a variable and thereby cause a NameError.


>> How the Python implementation implements that is an implementation detail.
>>
>> In short, how CPython does things is completely irrelevant to the language's
> semantics, so you're conflating things here.
>>
>
> As I'd understood the previous discussion, it is the CPython implementation
> that reserves local names and produces UnboundLocalErrors. The language
> semantics don't call for it, and another implementation might choose to handle
> function locals the same way as globals, through a namespace dictionary -- in
> which case the variable *wouldn't* exist in any way, shape, or form until it
> was assigned to.
>
> What am I getting wrong here?

The bit about the language semantics not specifying the effect.

 From the 3.1.1 language reference §4.1:

"When a name is not found at all, a NameError exception is raised. If the name 
refers to a local variable that has not been bound, a UnboundLocalError 
exception is raised. UnboundLocalError is a subclass of NameError."

And it goes on to elaborate on that, a little later:

"If a name binding operation occurs anywhere within a code block, all uses of 
the name within the block are treated as references to the current block. This 
can lead to errors when a name is used within a block before it is bound. This 
rule is subtle. Python lacks declarations and allows name binding operations to 
occur anywhere within a code block. The local variables of a code block can be 
determined by scanning the entire text of the block for name binding operations."

In short, Stephen D'Aprano's remarks were technically on the spot, while Rhodri 
James's follow up, that it seems influenced your response, was meaningless 
mumbo-jumbo with strong emphasis added repeatedly to denials of reality.

This is the usual reaction of the religious when exposed to reality.


Cheers & hth.,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>



More information about the Python-list mailing list