Some language proposals.

Josiah Carlson jcarlson at nospam.uci.edu
Wed Feb 25 16:42:00 EST 2004


Paul,

Your outputs were all incorrect.

>> Could somebody point me to an explanation of why closures are broken
>> in this way in the first place, please ?
> 
> 
> a = 5
> def b():
>     a = 6
>     print a
> b()
> print a
> 
> What will this print?
> 
> 5
> 6

 >>> a = 5
 >>> def b():
...     a = 6
...     print a
...
 >>> b()
6
 >>> print a
5


> Okay, then.
> 
> a = 5
> def b():
>     a = 6
>     def c():
>         a = 7
>         print a
>     print a
>     c()
> 
> print b()()
> print a
> 
> What does this print?
> 
> 7
> 6
> 5

What you give doesn't work, here's a slight modification:

 >>> a = 5
 >>> def b():
...     a = 6
...     def c():
...         a = 7
...         print a
...     print a
...     return c
...
 >>>
 >>> b()()
6
7
 >>> print a
5


I don't know what you were trying to prove, but next time, test it out.

  - Josiah



More information about the Python-list mailing list