python newbie
Bjoern Schliessmann
usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Fri Nov 2 17:46:14 EDT 2007
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? :)
>> 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
> >>> 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.
That's also true for translating to C/C++ nomenclature
where "declare" means "tell the compiler a name and its type"
and "define" means "declare and reserve memory for it".
> Hem... I'm not sure you're really addressing the OP's question
> here.
Me too ...
> 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".
>> They are.
>
> functions are *not* methods of their module.
Yes, I used the term very sloppily. At least they are attributes.
Regards,
Björn
--
BOFH excuse #450:
Terrorists crashed an airplane into the server room, have to
remove /bin/laden. (rm -rf /bin/laden)
More information about the Python-list
mailing list