static variables

Chris Angelico rosuav at gmail.com
Wed Dec 2 04:23:22 EST 2015


On Wed, Dec 2, 2015 at 7:21 PM, Antoon Pardon
<antoon.pardon at rece.vub.ac.be> wrote:
> I think python is unsuited for an obvious solution for static locals.
> Because you need to initialise your static variable somewhere. If you
> initialise whithin the body of your function, you will have a statement
> that is essentialy a declaration instead of an executable statement.
> Which goes totally against the dynamic nature op python.

It ought to be initialized at the same time the function is defined -
just like argument defaults, only without them being visible as
arguments. If Python had a keyword that meant
"currently-executing-function", that would work out well for
attributes (and might also make recursion more optimizable);
otherwise, default args are probably the cleanest way we have.

ChrisA



More information about the Python-list mailing list