473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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_list = []
def real_function(args):
pseudo_static_list.append(args)
print pseudo_static_list
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_args, _hidden_arg=[]):
_hidden_arg.append(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 3909

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

Similar topics

3
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"...
5
by: TomislaW | last post by:
What is the purpose or difference between private static and private method in non-static class?
8
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...
5
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...
16
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...
0
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...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.