Re: C-style static variables in Python?

2010-04-15 Thread Albert van der Horst
In article , Steve Holden wrote: >Terry Reedy wrote: >> On 4/1/2010 6:34 PM, kj wrote: >>> >>> >>> When coding C I have often found static local variables useful for >>> doing once-only run-time initializations. For example: >>> >>> int foo(int x, int y, int z) { >>> >>>static int first_time

Re: C-style static variables in Python?

2010-04-05 Thread Patrick Maupin
On Apr 5, 6:50 pm, Ethan Furman wrote: (Posted some code with a timeit...) Well, I'm not going to debug this, but with the *original* thing you posted, and the thing I posted, with a call and everything (more realistic scenario), the exception version seems slower on my machine: #!/usr/bin/env

Re: C-style static variables in Python?

2010-04-05 Thread Ethan Furman
Ethan Furman wrote: Steven D'Aprano wrote: On Fri, 02 Apr 2010 19:48:59 -0700, Ethan Furman wrote: The heuristic I use is, if I expect the try block to raise an exception more than about one time in ten, I change to an explicit test. In this case, since the exception should only be raised onc

Re: C-style static variables in Python?

2010-04-05 Thread Lee Harr
> Another approach would be to stuff the static values in the function's > __dict__. That's how I did it when I wanted something similar. I created this decorator: def static(**kw):     '''     Used to create a decorator function that will add an     attribute to a function and initialize it.

Re: C-style static variables in Python?

2010-04-04 Thread Patrick Maupin
On Apr 4, 1:57 pm, John Nagle wrote: >     If you want functions with state, use an object. That's what they're > for.  Don't muck with the internal representation of functions. > While "Don't muck with the internal representation of functions" is excellent advice over 99% of the time, it is also

Re: C-style static variables in Python?

2010-04-04 Thread John Nagle
kj wrote: When coding C I have often found static local variables useful for doing once-only run-time initializations. If you want functions with state, use an object. That's what they're for. Don't muck with the internal representation of functions. John N

Re: C-style static variables in Python?

2010-04-03 Thread Stephen Hansen
On 2010-04-02 20:24:46 -0700, Patrick Maupin said: On Apr 2, 10:11 pm, Stephen Hansen wrote: I don't know if properties are really faster or slower then a __getattr__, but I find them a lot cleaner if I want to delay some calculation until needed like that. Well, the relative speed of prope

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
Steven D'Aprano wrote: On Fri, 02 Apr 2010 19:48:59 -0700, Ethan Furman wrote: The heuristic I use is, if I expect the try block to raise an exception more than about one time in ten, I change to an explicit test. In this case, since the exception should only be raised once, and then never aga

Re: C-style static variables in Python?

2010-04-02 Thread Steven D'Aprano
On Fri, 02 Apr 2010 19:48:59 -0700, Ethan Furman wrote: >> The heuristic I use is, if I expect the try block to raise an exception >> more than about one time in ten, I change to an explicit test. In this >> case, since the exception should only be raised once, and then never >> again, I would use

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 10:11 pm, Stephen Hansen wrote: > > I don't know if properties are really faster or slower then a > __getattr__, but I find them a lot cleaner if I want to delay some > calculation until needed like that. Well, the relative speed of properties vs. __getattr__ can become irrelevant in at

Re: C-style static variables in Python?

2010-04-02 Thread Stephen Hansen
On 2010-04-02 19:42:29 -0700, Ethan Furman said: Terry Reedy wrote: In Duncan Booth writes: class Spam(object): mongo = None def __call__(self, x, y, z): if self.mongo is None: self.mongo = heavy_lifting_at_runtime() return frobnicate(x, y, z, self.mongo) Unless one wants the intializat

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
Steven D'Aprano wrote: On Fri, 02 Apr 2010 12:39:16 -0700, Patrick Maupin wrote: On Apr 2, 2:38 pm, Ethan Furman wrote: [...] Sounds like a personal preference issue, rather than a necessary / unnecessary issue -- after all, if you call that function a thousand times, only once is mongo n

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
Terry Reedy wrote: In Duncan Booth writes: class Spam(object): mongo = None def __call__(self, x, y, z): if self.mongo is None: self.mongo = heavy_lifting_at_runtime() return frobnicate(x, y, z, self.mongo) Unless one wants the intialization of mongo delayed

Re: C-style static variables in Python?

2010-04-02 Thread Terry Reedy
On 4/2/2010 1:28 PM, Paul McGuire wrote: On Apr 1, 5:34 pm, kj wrote: When coding C I have often found static local variables useful for doing once-only run-time initializations. For example: Here is a decorator to make a function self-aware, giving it a "this" variable that points to itsel

Re: C-style static variables in Python?

2010-04-02 Thread Terry Reedy
On 4/2/2010 6:59 PM, kj wrote: In Duncan Booth writes: class Spam(object): mongo = None def __call__(self, x, y, z): if self.mongo is None: self.mongo = heavy_lifting_at_runtime() return frobnicate(x, y, z, self.mongo) Unless one wants the intialization of mo

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 6:57 pm, Steven D'Aprano wrote: > On Fri, 02 Apr 2010 12:39:16 -0700, Patrick Maupin wrote: > > On Apr 2, 2:38 pm, Ethan Furman wrote: > [...] > >> Sounds like a personal preference issue, rather than a necessary / > >> unnecessary issue -- after all, if you call that function a thousan

Re: C-style static variables in Python?

2010-04-02 Thread Steven D'Aprano
On Fri, 02 Apr 2010 12:39:16 -0700, Patrick Maupin wrote: > On Apr 2, 2:38 pm, Ethan Furman wrote: [...] >> Sounds like a personal preference issue, rather than a necessary / >> unnecessary issue -- after all, if you call that function a thousand >> times, only once is mongo not defined... clearl

Re: C-style static variables in Python?

2010-04-02 Thread kj
In Duncan Booth writes: >class Spam(object): > mongo = None > def __call__(self, x, y, z): > if self.mongo is None: > self.mongo = heavy_lifting_at_runtime() > return frobnicate(x, y, z, self.mongo) >spam = Spam() >ham = spam(1, 2, 3) I really like this. Thanks. >T

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 3:33 pm, Ethan Furman wrote: > My main point, though, was using __call__, and not some weird _ method.  ;) Yes, __call__ is good. In general, not naming things that don't need to be named is good (but if you have too many of them to keep track of, then, obviously, they need to be named

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
Patrick Maupin wrote: [snippage] Well, I think the whole discussion has basically been about personal preference. OTOH, but if you call the function a few million times, you might find the cost of try/except to be something that you would rather not incur -- it might become a performance issue

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 2:38 pm, Ethan Furman wrote: > Patrick Maupin wrote: > > On Apr 2, 1:21 pm, Ethan Furman wrote: > >> For this type of situation, my preference would be: > > >> class spam(object): > >>      def __call__(self, x, y, z): > >>          try: > >>              mongo = self.mongo > >>        

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
Patrick Maupin wrote: On Apr 2, 1:21 pm, Ethan Furman wrote: For this type of situation, my preference would be: class spam(object): def __call__(self, x, y, z): try: mongo = self.mongo except AttributeError: mongo = self.mongo = heavy_lifting_a

Re: C-style static variables in Python?

2010-04-02 Thread Steven D'Aprano
On Fri, 02 Apr 2010 16:08:42 +, kj wrote: > Other responses advocated for global variables. I avoid them in > general, In general this is wise, but remember that because Python globals are not globally global, but local to a single module, they're safer than globals in other languages. St

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 1:21 pm, Ethan Furman wrote: > For this type of situation, my preference would be: > > class spam(object): >      def __call__(self, x, y, z): >          try: >              mongo = self.mongo >          except AttributeError: >              mongo = self.mongo = heavy_lifting_at_runtime(

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
kj wrote: class _Spam(object): @classmethod def _(cls, x, y, z): try: mongo = cls.mongo except AttributeError: mongo = cls.mongo = heavy_lifting_at_runtime() return frobnicate(x, y, z, mongo) ham = _Spam._(1, 2, 3) Is this really more n

Re: C-style static variables in Python?

2010-04-02 Thread Duncan Booth
kj wrote: > I suppose one could refactor this: > > > def spam(x, y, z): > try: > mongo = spam.mongo > except AttributeError: > mongo = spam.mongo = heavy_lifting_at_runtime() > return frobnicate(x, y, z, mongo) > > ham = spam(3, 4, 5) > > > into this: > > > class

Re: C-style static variables in Python?

2010-04-02 Thread Mel
kj wrote: > In Steve Holden > writes: > >>But the real problem is that the OP is insisting on using purely >>procedural Python when the problem is screaming for an object-oriented >>answer. > > My initial reaction to this comment was something like "What? switch > from procedural to OO just to

Re: C-style static variables in Python?

2010-04-02 Thread Paul McGuire
On Apr 1, 5:34 pm, kj wrote: > When coding C I have often found static local variables useful for > doing once-only run-time initializations.  For example: > Here is a decorator to make a function self-aware, giving it a "this" variable that points to itself, which you could then initialize from

Re: C-style static variables in Python?

2010-04-02 Thread kj
In Steve Holden writes: >But the real problem is that the OP is insisting on using purely >procedural Python when the problem is screaming for an object-oriented >answer. My initial reaction to this comment was something like "What? switch from procedural to OO just to be able to do some one-t

Re: C-style static variables in Python?

2010-04-01 Thread Paul Rubin
kj writes: > When coding C I have often found static local variables useful for > doing once-only run-time initializations. For example: > > int foo(int x, int y, int z) { > static int first_time = TRUE; > static Mongo *mongo; > if (first_time) { ... Here are some cheesy ways. 1. Put an

Re: C-style static variables in Python?

2010-04-01 Thread Alf P. Steinbach
* kj: When coding C I have often found static local variables useful for doing once-only run-time initializations. For example: int foo(int x, int y, int z) { static int first_time = TRUE; static Mongo *mongo; if (first_time) { mongo = heavy_lifting_at_runtime(); first_time = FAL

Re: C-style static variables in Python?

2010-04-01 Thread Steve Holden
Terry Reedy wrote: > On 4/1/2010 6:34 PM, kj wrote: >> >> >> When coding C I have often found static local variables useful for >> doing once-only run-time initializations. For example: >> >> int foo(int x, int y, int z) { >> >>static int first_time = TRUE; >>static Mongo *mongo; >>if

Re: C-style static variables in Python?

2010-04-01 Thread Patrick Maupin
On Apr 1, 6:10 pm, Steve Holden wrote: > Chris Rebert wrote: > > Personally, I hate such abuse with a passion; I think a global > > variable is clearest. > > But the real problem is that the OP is insisting on using purely > procedural Python when the problem is screaming for an object-oriented >

Re: C-style static variables in Python?

2010-04-01 Thread Terry Reedy
On 4/1/2010 6:34 PM, kj wrote: When coding C I have often found static local variables useful for doing once-only run-time initializations. For example: int foo(int x, int y, int z) { static int first_time = TRUE; static Mongo *mongo; if (first_time) { mongo = heavy_lifting_at_

Re: C-style static variables in Python?

2010-04-01 Thread Steve Holden
Chris Rebert wrote: > On Thu, Apr 1, 2010 at 3:34 PM, kj wrote: >> When coding C I have often found static local variables useful for >> doing once-only run-time initializations. > >> Another approach would be to stuff the static values in the function's >> __dict__. This is less satisfactory th

Re: C-style static variables in Python?

2010-04-01 Thread Chris Rebert
On Thu, Apr 1, 2010 at 3:34 PM, kj wrote: > When coding C I have often found static local variables useful for > doing once-only run-time initializations. > Another approach would be to stuff the static values in the function's > __dict__.  This is less satisfactory than the closure approach > be

C-style static variables in Python?

2010-04-01 Thread kj
When coding C I have often found static local variables useful for doing once-only run-time initializations. For example: int foo(int x, int y, int z) { static int first_time = TRUE; static Mongo *mongo; if (first_time) { mongo = heavy_lifting_at_runtime(); first_time = FALSE;