python newbie
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Nov 2 12:37:51 EDT 2007
Bjoern Schliessmann a écrit :
> Jim Hendricks wrote:
(snip)
>> I see the global keyword that allows access to global vars in a
>> function, what I'm not clear on is does that global need to be
>> declared in the global scope,
>
> 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.
> Yes, globals need to be defined before you
> can access them using "global".
For which definition of 'defined' ?
>>> p
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'p' is not defined
>>> toto()
>>> p
42
>>>
>> 2) Everything is an object. So then, why the distinction between
>> functions/variables and fields/methods.
>
> Because they are different things to achieve different goals. One
> holds data, the other does stuff.
Hem... I'm not sure you're really addressing the OP's question here. 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.
(snip)
>> If a module is an object, would not every function be a method of
>> that module and every variable be a field of that module?
>
> They are.
functions are *not* methods of their module.
More information about the Python-list
mailing list