too simple a question : forward declaration?

Bengt Richter bokr at oz.net
Wed May 14 21:11:22 EDT 2003


On Wed, 14 May 2003 11:47:12 GMT, Alex Martelli <aleax at aleax.it> wrote:
[...]
>Forget the existence of 'declarations' in other
>languages.  In Python they do not exist (the 'global'
>statement comes closest, but it has nothing to do
>with the 'declaration of functions' anyway).
>
Though (this is a nit ;-) it can affect the binding when the a def is executed:

 >>> def foo():
 ...     global bar
 ...     def bar(): print 'bar here'
 ...
 >>> bar()
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 NameError: name 'bar' is not defined
 >>> foo()
 >>> bar()
 bar here

Another nit: explicit vs indirect use of a variable locally can sort of
"declare" something about it, like "global" but sort of different ;-/
(Note that is not strictly the old local vs global thing).

 >>> def foo():
 ...     y =456
 ...     def bar():
 ...         print locals().get('y', 'no "y" seen')
 ...     bar()
 ...
 >>> foo()
 no "y" seen
 >>> def foo():
 ...     y =456
 ...     def bar():
 ...         print locals().get('y', 'no "y" seen')
 ...         dummy=y
 ...     bar()
 ...
 >>> foo()
 456

Regards,
Bengt Richter




More information about the Python-list mailing list