python newbie

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Nov 4 15:04:07 CET 2007


Bjoern Schliessmann a écrit :
> Bruno Desthuilliers wrote:
> 
>>Bjoern Schliessmann a écrit :
> 
> 
>>>You can't just declare in Python, you always define objects (and
>>>bind a name to them).
>>
>>def toto():
>>   global p
>>   p = 42
>>
>>Here I declared 'x' as global without defining it.
> 
> 
> Ah well, someone had to notice it ...
> 
> BTW, where's x? :)

Sorry. s/x/p/g, of course.

> 
>>>Yes, globals need to be defined before you
>>>can access them using "global".
>>
>>For which definition of 'defined' ?
> 
> 
> to define a name: to bind an object to a name

Then - according to this definition - your assertion is false.

> 
>> >>> p
>>Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>>NameError: name 'p' is not defined
>> >>> toto()
>> >>> p
>>42
>> >>>
> 
> 
> Easy, p = 42 is a definition. 

Yes, but it came *after* the global statement. So the global statement 
*is* a kind of declaration, and the 'subject' of this declaration 
doesn't need to be previously defined.


> 
>>But anyway: in Python, everything's an object, so the only 
>>thing that makes functions a bit specials is that they are
>>callable - as are classes, methods, and every instance of a class
>>implementing __call__ FWIW. So saying 'variables holds data,
>>functions do stuff' is unapplyiable to Python.
> 
> 
> I don't think so. Even if everything is an object, there is still a
> concept behind the words "function" and "variable".

f = lambda x: x+2

def toto(c):
   return c(21)

toto(f)

What's f ? A function or a variable ?-)



More information about the Python-list mailing list