Values and objects

Ethan Furman ethan at stoneleaf.us
Sat May 10 21:28:43 EDT 2014


On 05/10/2014 04:18 PM, Chris Angelico wrote:
> On Sun, May 11, 2014 at 5:10 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> And if you don't like that argument (although it is a perfectly sound and
>> correct argument), think of the module name space:
>>
>>
>> ret = spam
>> spam = 23
>>
>> will net you a simple NameError, because spam has not yet been created.
>
> What about this, though:
>
> ret = int
> int = 23
>
> That will *not* net you a NameError, because 'int' exists in an outer
> scope (builtins). You can create a new module-scope variable and it
> will immediately begin to shadow a builtin; you can delete that
> variable and it will immediately cease to shadow that builtin. That's
> the difference I'm talking about. With function-local variables, they
> all have to exist (as other responses confirmed, that *is* a language
> guarantee), even though some of them aren't bound to anything yet.

Well, with function variables they have to exist *when you use them*. ;)

This seems like more of a scoping issue than a "can we create variables in Python" issue.

I am curious, though, what other python's do with respect to function variables.

--
~Ethan~



More information about the Python-list mailing list