Static (local) Method Variables (was: can someone explain?)

Mongryong Mongryong at sympatico.ca
Tue Feb 18 11:09:30 EST 2003


On Tue, 2003-02-18 at 01:57, Jp Calderone wrote:
> On Tue, Feb 18, 2003 at 12:49:18AM -0500, Mongryong wrote:
> > Some people have asked about how to do static (local) method variables
> > (like in C/C++).  Apparently, there is a simple way to do it via default
> > function parameters.  Consider the following example:
> > 
> > def append(x, list=[]):
> > 	list += x
> > 	return list
> > 
> > >>> append(1)
> > [1]
> > >>> append(2)
> > [1,2]
> > 
> > Now, the above isn't really a 'true' immitation of C/C++'s static method
> > variable implementation.  A true static method variable is created at
> > first call.  In Python, default parameters are created at 'import
> > time'.  Hence, you're not saving any 'memory space' like you would in
> > C/C++.
> 
>   Default parameters are *evaluated* at function definition time.  This is
> ocassionally the same as "import time", but not always.
> 
>   Since we're discussing free functions, there is no memory saved in either
> Python or C.  If we were discussing static class members, C++ style, then a
> far more appropriate comparison is Python class attributes, which are
> "shared" amongst all instances of a class, just as C++'s static class
> members are.
> 
> > 
> > Note, the above only works for referencable objects and not built in
> > types like 'ints'.
> > 
> 
>   This is wrong.  All objects in Python are "referencable".  What do you
> think "x = 5" does?  I believe you are suffering from the common confusion
> about mutable vs immutable objects.

I think you're getting 'programming language semantics' confused with
'how the programming language is implemented.'

If you're getting into the gritty details about Python implementation,
then 'every variable' is being passed by a pointer - which is different
than passed-by-value and pass-by-reference.  Passed-by-value implies a
'copy' of a variable is passed on to the function being called.  Passing
a pointer is 'almost' the same as pass-by-reference.  Pass-by-reference
and passing a pointer are more or less than same when the pointer is
valid.  

If you're talking about the 'programmer's point of view' (language
semantics) where you don't know what's going on under the covers, in
Python, simple types 'behave like they're passed-by-value' and complex
types 'behave like they're passed-by-reference'.  The fact that Python
actually passes all variables by pointers (ie. to some PyObject...) and
protects 'immutable' types from being modified is all hidden to the
programmer.  In fact, I don't think Python even has a 'immutable'
keyword.  If Python had keywords like 'immutable' and 'mutable' (const
and mutable in C++), then from the 'programmer's point of view'
everything 'is' and 'behaves' as being passed by reference but variables
declared as immutable can not be modified.

Of course, I might be suffering from the fact that I keep thinking
Python's a real programming language - but its really just a scripting
language.




> 
>   Jp
> 







More information about the Python-list mailing list