Static (local) Method Variables (was: can someone explain?)
Jp Calderone
exarkun at intarweb.us
Tue Feb 18 01:57:58 EST 2003
More information about the Python-list mailing list
Tue Feb 18 01:57:58 EST 2003
- Previous message (by thread): Static (local) Method Variables (was: can someone explain?)
- Next message (by thread): Static (local) Method Variables (was: can someone explain?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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. Jp -- up 9 days, 10:28, 3 users, load average: 0.00, 0.02, 0.03 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 196 bytes Desc: not available URL: <http://mail.python.org/pipermail/python-list/attachments/20030218/ea1ff770/attachment.sig>
- Previous message (by thread): Static (local) Method Variables (was: can someone explain?)
- Next message (by thread): Static (local) Method Variables (was: can someone explain?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list