[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Another dumb scope question for a closure.

Fredrik Lundh

1/9/2008 8:55:00 PM

Ben Fisher wrote:

> One way to get this to work is:
>
> def inc(jj):
> def dummy(jj = jj):
> jj = jj + 1
> return jj
> return dummy
>
> h = inc(33)
> print h()
>
> It's not very pretty though, especially when you have many variables
> you want to have in the inner scope.

Default argument binding allows you to bind to an outer object rather
than a name, but it doesn't help if you want to update the object:

>>> def inc(jj):
.... def dummy(jj = jj):
.... jj = jj + 1
.... return jj
.... return dummy
....
>>> h = inc(33)
>>> print h()
34
>>> print h()
34
>>> print h()
34
>>> print h()
34

</F>