Overriding a global

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Dec 13 08:30:42 EST 2011


Steven D'Aprano wrote:
> On Tue, 13 Dec 2011 10:54:51 +0100, Jean-Michel Pichavant wrote:
>
>   
>> Steven D'Aprano wrote:
>>     
>>> On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote:
>>>
>>>
>>>       
>>>> Using the same name for 2 different objects is a bad idea in general.
>>>>     
>>>>     
>>>>         
>>> We have namespaces precisely so you don't need to care about making
>>> names globally unique.
>>>
>>>
>>>
>>>       
>> I don't get your point, namespaced names are unique, by definition.
>>
>> foo.aname <> bar.aname
>>     
>
> Assuming foo and bar are not names for the same object, there are at 
> least three namespaces here: the local namespace, where foo and bar can 
> be found, the foo.__dict__ namespace, and the bar.__dict__ namespace.
>
>
>   
>> The OP showed a code where there was a confusion between a global name
>> and a local one. There's no namespace involved. Having a local name
>> identical to a global one is a bad idea, def.
>>     
>
> Of course there are namespaces involved. There is the global namespace, 
> and the local namespace. That's how you can have x inside a function 
> without it overwriting global x outside of it, because they are different 
> namespaces. Which is my point.
>
> When I write this:
>
> x = 1
>
> def spam():
>     x = 2
>
> def ham():
>     x = 3
>
> The three x's don't clash because they are in three separate namespaces.
>
>   
I know that, why do I have the feel this is juste a semantic issue ?
Aren't you/I/we confusion namespaces & scopes ?

Anyway, semantic is not what we're interested in here.

writing

x = 1

def spam():
    x = 2


is in general a bad idea. That was my point.

JM






More information about the Python-list mailing list