why function got dictionary
bruno.desthuilliers at gmail.com
bruno.desthuilliers at gmail.com
Sat Apr 19 14:33:41 EDT 2008
On 19 avr, 19:39, sturlamolden <sturlamol... at yahoo.no> wrote:
> On Apr 17, 4:06 pm, AlFire <spamgrinder.tryla... at gmail.com> wrote:
>
> > Q: why function got dictionary? What it is used for?
>
> As previously mentioned, a function has a __dict__ like (most) other
> objects.
>
> You can e.g. use it to create static variables:
>
> int foobar()
> {
> static int i = 0;
> return i++;
>
> }
>
> is roughly equivalent to:
>
> def foobar():
> foobar.i += 1
> return foobar.i
> foobar.i = 0
barfoo = foobar
foobar = lambda x : x
And boom.
'static' variables are better implemented using either closures,
mutable default arguments or custom callable types.
More information about the Python-list
mailing list