473,563 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: using "private" parameters as static storage?



Quoting Joe Strout <jo*@strout.net >:
On Nov 13, 2008, at 10:19 AM, Chris Mellon wrote:
Static storage is a way of preserving state. Objects are a way of
encapsulating state and behavior. Use an object.
Argh. I've been back in the Python community for about a month, and
I've been continually amazed at how every single "how do I do X" or
"what do you think of this method of doing X" question is answered by
people on high horses claiming "you shouldn't do X".

I know very well about state and objects. I'll be happy to whip out
my software engineering credentials and measure them against yours if
that's how you like to play.
I'm sure your credentials are bigger than mine. But he is right. A lot of
languages have ditched the "concept" of a static variable on a method (how do
you parse that sentence, btw?) in favour of using encapsulation. Of the top of
my head, there is Java, C# and Python, and I think even PHP and Perl. They call
them "private variables", including the
name-mangled-publicly-accessible-__python's variables.

I only know one family of languages that support it: C/C++ (LISP, maybe? I don't
remember - my list is very sketchy), but here comes into play the fact that your
credentials are bigger. But that may be the reason why you refuse to follow yet
another model.

Python doesn't have the concept of 'static variables', as in "a variable that
remembers its value between function calls". Instance attributes (or class
atributes, or metaclass attributes - your choice, depending on the problem)
exist for that.

So, if you want to simulate your C++ static variables in python, you ha ve a few
choices (more than in other languages, btw):
* Global variable. Ouch. We really don't want you going there :D
* Instance variable. Maybe a __private instance variable, to avoid polluting
your descendant's namespaces.
* Class/Metaclass variables (if you need them - that's rare).
* Closures
* Default arguments (too fragile for my taste)
* Instance variables _of the function object_.

For the last three: (untested code follows)

* With closures:

===
def myfunc():
pseudo_static_l ist = []
def real_function(a rgs):
pseudo_static_l ist.append(args )
print pseudo_static_l ist
return real_function
myfunc = myfunc()
===

[or, until python removes the apply function]

===
@apply
def myfunc():
# same definition as before
return real_function
===

Caveat: you cannot assign, as in python 2.5 at least, to the closure variable -
doing so would create a local variable instead.

* With default argument:
===
def myfunc(normal_a rgs, _hidden_arg=[]):
_hidden_arg.app end(normal_args )
print _hidden arg
===
(You can't shouldn't to _hidden_arg either, and you risk someone invokingthe
function with that argument)

* With function's instance members:
===
def myfunc(args):
myfunc.counter= 1 # well, you should use some logic to see if it's not
# initialized
myfunc.counter+ =1
print myfunc.counter
===

Instance attributes beats them all, though.
I understand very well when data should
be stored as instance data, and when it should be instead tucked away
as static data within a method.
OT: Please enlighthen me. I didn't grew with C, and even when I saw C++,
instance variables made a lot more sense to me that 'static' variables.
If you don't understand that, or are
happy without having the choice, and have no answer to the question I
was asking, then that's fine.
I believe he had an answer... You didn't like it, though. I hope mine wasmore
palatable to you.

--
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie
Nov 14 '08 #1
0 3983

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2096
by: | last post by:
I know that MS VC++6 has problems in this syntax: --------------------------------- class A { private: static const int a = 5; } --------------------------------- The hack is to use "enum" instead, but in my case below I don't know what to do: --------------------------------- class A {
5
1639
by: TomislaW | last post by:
What is the purpose or difference between private static and private method in non-static class?
8
2609
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new Object(); ...// some codes
5
1964
by: Stodge | last post by:
I've exposed a C++ class to Python using Boost Python. The class, let's say it's called Entity, contains private static data, which is an array of strings. Though I think it implements it using MFC's CPtrArray. I've also exposed a public function from Entity - let's say it's called foo. This function accesses the private static data (the...
16
2033
by: Joe Strout | last post by:
One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method -- i.e. storage that is persistent between calls, but not visible outside the method. I frequently use this for such things as caching, or for keeping track of how many objects a factory function has created, and so on. Today it...
0
371
by: J. Cliff Dyer | last post by:
On Thu, 2008-11-13 at 11:19 -0600, Chris Mellon wrote: He is using an object. Specifically, he's using a function object. Though perhaps you meant put it into a class. Here are a few essays into the matter .... foo._count += 1 .... return ("spam " * foo.count).rstrip() ....
1
6840
by: romand | last post by:
EDIT: ***SOLUTION FOUND*** Just to check, before I had the 2 functions in the second code box I used in the main the logger function and when I got it to be private I haven't erased the lines in the main. ********* Question: hello, If you remember me from yesterday, having some problems. I've defined a helpFunctions.cpp class with static...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.