Re: C-style static variables in Python?

2010-04-15 Thread Albert van der Horst
In article mailman.1441.1270165718.23598.python-l...@python.org, Steve Holden st...@holdenweb.com 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

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-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

Re: C-style static variables in Python?

2010-04-05 Thread Patrick Maupin
On Apr 5, 6:50 pm, Ethan Furman et...@stoneleaf.us 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

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

Re: C-style static variables in Python?

2010-04-04 Thread Patrick Maupin
On Apr 4, 1:57 pm, John Nagle na...@animats.com 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

Re: C-style static variables in Python?

2010-04-03 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 a

Re: C-style static variables in Python?

2010-04-03 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

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 apt.shan...@gmail.invalid 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,

Re: C-style static variables in Python?

2010-04-02 Thread kj
In mailman.1437.1270163476.23598.python-l...@python.org Steve Holden st...@holdenweb.com 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

Re: C-style static variables in Python?

2010-04-02 Thread Paul McGuire
On Apr 1, 5:34 pm, kj no.em...@please.post 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

Re: C-style static variables in Python?

2010-04-02 Thread Mel
kj wrote: In mailman.1437.1270163476.23598.python-l...@python.org Steve Holden st...@holdenweb.com 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

Re: C-style static variables in Python?

2010-04-02 Thread Duncan Booth
kj no.em...@please.post wrote: I suppose one could refactor this: procedural 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) /procedural

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
kj wrote: OO 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) /OO Is this really

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 1:21 pm, Ethan Furman et...@stoneleaf.us 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 =

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.

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
Patrick Maupin wrote: On Apr 2, 1:21 pm, Ethan Furman et...@stoneleaf.us 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 =

Re: C-style static variables in Python?

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

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

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 3:33 pm, Ethan Furman et...@stoneleaf.us 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,

Re: C-style static variables in Python?

2010-04-02 Thread kj
In xns9d4ec021dc8eaduncanbo...@127.0.0.1 Duncan Booth duncan.bo...@invalid.invalid 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 =

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 et...@stoneleaf.us 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

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 6:57 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: On Fri, 02 Apr 2010 12:39:16 -0700, Patrick Maupin wrote: On Apr 2, 2:38 pm, Ethan Furman et...@stoneleaf.us wrote: [...] Sounds like a personal preference issue, rather than a necessary / unnecessary issue --

Re: C-style static variables in Python?

2010-04-02 Thread Terry Reedy
On 4/2/2010 6:59 PM, kj wrote: Inxns9d4ec021dc8eaduncanbo...@127.0.0.1 Duncan Boothduncan.bo...@invalid.invalid writes: class Spam(object): mongo = None def __call__(self, x, y, z): if self.mongo is None: self.mongo = heavy_lifting_at_runtime() return

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, kjno.em...@please.post 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

Re: C-style static variables in Python?

2010-04-02 Thread Ethan Furman
Terry Reedy wrote: Inxns9d4ec021dc8eaduncanbo...@127.0.0.1 Duncan Boothduncan.bo...@invalid.invalid 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,

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 et...@stoneleaf.us wrote: [...] Sounds like a personal preference issue, rather than a necessary / unnecessary issue -- after all, if you call that function a thousand times,

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: Inxns9d4ec021dc8eaduncanbo...@127.0.0.1 Duncan Boothduncan.bo...@invalid.invalid writes: class Spam(object): mongo = None def __call__(self, x, y, z): if self.mongo is None: self.mongo = heavy_lifting_at_runtime() return

Re: C-style static variables in Python?

2010-04-02 Thread Patrick Maupin
On Apr 2, 10:11 pm, Stephen Hansen apt.shan...@gmail.invalid 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

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;

Re: C-style static variables in Python?

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

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 no.em...@please.post wrote: When coding C I have often found static local variables useful for doing once-only run-time initializations. snip Another approach would be to stuff the static values in the function's __dict__. This is less

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 =

Re: C-style static variables in Python?

2010-04-01 Thread Patrick Maupin
On Apr 1, 6:10 pm, Steve Holden st...@holdenweb.com 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

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 (first_time) {

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 =

Re: C-style static variables in Python?

2010-04-01 Thread Paul Rubin
kj no.em...@please.post 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