Re: using "private" parameters as static storage?

2008-11-14 Thread Aaron Brady
On Nov 13, 4:23 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > def static(**vars): >     ns = NS(vars) >     def deco(f): >         return lambda *args, **kwargs: f(ns, *args, **kwargs) >     return deco > > @static(ncalls=0, history=[]) > def foo(ns, x): >    ns.ncalls += 1 >    ns.history.appe

Re: using "private" parameters as static storage?

2008-11-14 Thread Arnaud Delobelle
Joe Strout <[EMAIL PROTECTED]> writes: > On Nov 13, 2008, at 3:23 PM, Arnaud Delobelle wrote: > >> Aaron Brady <[EMAIL PROTECTED]> writes: >> >>> One way around it, which I like the idea of but I'll be honest, I've >>> never used, is getting a function a 'self' parameter. You could make >>> it a

Re: using "private" parameters as static storage?

2008-11-14 Thread Joe Strout
On Nov 13, 2008, at 8:26 PM, Steven D'Aprano wrote: def spam(_count=[0]): _count[0] += 1 return "spam " * _count[0] This is a common trick, often used for things like caching. One major advantage is that you are exposing the cache as an *optional* part of the interface, which make

Re: using "private" parameters as static storage?

2008-11-14 Thread Joe Strout
On Nov 13, 2008, at 3:23 PM, Arnaud Delobelle wrote: Aaron Brady <[EMAIL PROTECTED]> writes: One way around it, which I like the idea of but I'll be honest, I've never used, is getting a function a 'self' parameter. You could make it a dictionary or a blank container object, or just the funct

Re: using "private" parameters as static storage?

2008-11-14 Thread Chris Mellon
On Thu, Nov 13, 2008 at 10:25 PM, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Fri, 14 Nov 2008 13:35:02 +1100, Ben Finney wrote: > >> Instead, it looks like you're falling foul of one of the classic >> mistakes in the "How to ask questions the smart way" document: you've >> got a goal, but you'

Re: using "private" parameters as static storage?

2008-11-14 Thread Bruno Desthuilliers
Matimus a écrit : On Nov 13, 9:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: (snip) def spam2(): if not hasattr(spam2,'count'):spam2.count=0 spam2.count += 1 return "spam2 " * spam2.count This is definitely preferred over the first. I beg to disagree. This solution stores

Re: using "private" parameters as static storage?

2008-11-14 Thread Luis Zarrabeitia
Quoting Joe Strout <[EMAIL PROTECTED]>: > Hi Luis, > > > 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. > > A static variable IS encapsulation. Encapsulation happens at many >

Re: using "private" parameters as static storage?

2008-11-14 Thread Bruno Desthuilliers
Joe Strout a écrit : One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method s/method/function/ -- 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 ke

Re: using "private" parameters as static storage?

2008-11-14 Thread Peter Otten
Steven D'Aprano wrote: > On Thu, 13 Nov 2008 19:52:13 -0700, Joe Strout wrote: > >> Pity there isn't a way for a function to get a reference to itself >> except by name. Still, when you rename a method, you're going to have >> to update all the callers anyway -- updating a couple of extra >> ref

Re: using "private" parameters as static storage?

2008-11-14 Thread Aaron Brady
On Nov 13, 10:25 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Fri, 14 Nov 2008 13:35:02 +1100, Ben Finney wrote: > > Instead, it looks like you're falling foul of one of the classic > > mistakes in the “How to ask questions the smart way” document: you've > > got a goal, b

Re: using "private" parameters as static storage?

2008-11-13 Thread Steven D'Aprano
On Thu, 13 Nov 2008 18:57:37 -0800, alex23 wrote: > On Nov 14, 12:16 pm, Joe Strout <[EMAIL PROTECTED]> wrote: >> 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 doin

Re: using "private" parameters as static storage?

2008-11-13 Thread Steven D'Aprano
On Thu, 13 Nov 2008 19:52:13 -0700, Joe Strout wrote: > Pity there isn't a way for a function to get a reference to itself > except by name. Still, when you rename a method, you're going to have > to update all the callers anyway -- updating a couple of extra > references within the method is not

Re: using "private" parameters as static storage?

2008-11-13 Thread Steven D'Aprano
On Fri, 14 Nov 2008 13:35:02 +1100, Ben Finney wrote: > Instead, it looks like you're falling foul of one of the classic > mistakes in the “How to ask questions the smart way” document: you've > got a goal, but you're assuming that you need to use a specific tool to > get there. Instead, you're be

Re: using "private" parameters as static storage?

2008-11-13 Thread Steven D'Aprano
On Thu, 13 Nov 2008 14:25:08 -0800, Paul Boddie wrote: > On 13 Nov, 18:16, Joe Strout <[EMAIL PROTECTED]> wrote: >> 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 outs

Re: using "private" parameters as static storage?

2008-11-13 Thread Joe Strout
Hi Luis, 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. A static variable IS encapsulation. Encapsulation happens at many levels: module, class, instance, and (in languages that suppor

Re: using "private" parameters as static storage?

2008-11-13 Thread Steven D'Aprano
On Thu, 13 Nov 2008 13:35:21 -0500, Steve Holden wrote: > Joe Strout wrote: >> 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 >>

Re: using "private" parameters as static storage?

2008-11-13 Thread Steven D'Aprano
On Thu, 13 Nov 2008 14:05:17 -0500, Jean-Paul Calderone wrote: > On Thu, 13 Nov 2008 10:58:49 -0800 (PST), [EMAIL PROTECTED] wrote: >>On Nov 13, 11:32 am, "J. Cliff Dyer" <[EMAIL PROTECTED]> wrote: >>> On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote: >> [snip] >>> > Preserving state is what class

Re: using "private" parameters as static storage?

2008-11-13 Thread Steven D'Aprano
On Thu, 13 Nov 2008 10:16:59 -0700, Joe Strout wrote: > 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 ca

Re: using "private" parameters as static storage?

2008-11-13 Thread Arnaud Delobelle
Aaron Brady <[EMAIL PROTECTED]> writes: > One way around it, which I like the idea of but I'll be honest, I've > never used, is getting a function a 'self' parameter. You could make > it a dictionary or a blank container object, or just the function > itself. > > @self_param > def spam( self ): >

Re: using "private" parameters as static storage?

2008-11-13 Thread George Sakkis
On Nov 13, 9:22 pm, Joe Strout <[EMAIL PROTECTED]> wrote: > Steve wrote: > > This is a pretty bizarre requirement, IMHO. The normal place to keep > > such information is either class variables or instance variables. > > Holy cow, I thought it was just Chris, but there were half a dozen   > similar

Re: using "private" parameters as static storage?

2008-11-13 Thread Luis Zarrabeitia
Quoting Joe Strout <[EMAIL PROTECTED]>: > 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 >

Re: using "private" parameters as static storage?

2008-11-13 Thread alex23
On Nov 14, 12:16 pm, Joe Strout <[EMAIL PROTECTED]> wrote: > 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 hors

Re: using "private" parameters as static storage?

2008-11-13 Thread Joe Strout
On Nov 13, 2008, at 11:15 AM, J. Cliff Dyer wrote: Here are a few essays into the matter def foo(): ... foo._count += 1 ... return ("spam " * foo.count).rstrip() Simple and straightforward, and _count is still encapsulated in the function, but it's kind of ugly, because when the func

Re: using "private" parameters as static storage?

2008-11-13 Thread Ben Finney
Joe Strout <[EMAIL PROTECTED]> writes: > 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 b

Re: using "private" parameters as static storage?

2008-11-13 Thread Joe Strout
Steve wrote: This is a pretty bizarre requirement, IMHO. The normal place to keep such information is either class variables or instance variables. Holy cow, I thought it was just Chris, but there were half a dozen similar responses after that. I'm starting to see a pattern here... any tim

Re: using "private" parameters as static storage?

2008-11-13 Thread Joe Strout
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

Re: using "private" parameters as static storage?

2008-11-13 Thread Steve Holden
Ben Finney wrote: > Joe Strout <[EMAIL PROTECTED]> writes: > >> 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 > > This is precisely what classes are for: allowing functionality and >

Re: using "private" parameters as static storage?

2008-11-13 Thread Ben Finney
Joe Strout <[EMAIL PROTECTED]> writes: > 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 This is precisely what classes are for: allowing functionality and state to exist in a single obj

Re: using "private" parameters as static storage?

2008-11-13 Thread Aaron Brady
On Nov 13, 4:13 pm, Paul McGuire <[EMAIL PROTECTED]> wrote: > On Nov 13, 3:08 pm, Aaron Brady <[EMAIL PROTECTED]> wrote: > > > > > On Nov 13, 11:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: > > > > One thing I miss as I move from REALbasic to Python is the ability to   > > > have static storage wit

Re: using "private" parameters as static storage?

2008-11-13 Thread Paul Boddie
On 13 Nov, 18:16, Joe Strout <[EMAIL PROTECTED]> wrote: > 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 c

Re: using "private" parameters as static storage?

2008-11-13 Thread Paul McGuire
On Nov 13, 3:08 pm, Aaron Brady <[EMAIL PROTECTED]> wrote: > On Nov 13, 11:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: > > > > > > > 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 call

Re: using "private" parameters as static storage?

2008-11-13 Thread Aaron Brady
On Nov 13, 11:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: > 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 th

Re: using "private" parameters as static storage?

2008-11-13 Thread Steve Holden
Steve Holden wrote: > Joe Strout wrote: >> 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 f

Re: using "private" parameters as static storage?

2008-11-13 Thread rurpy
On Nov 13, 12:05 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote: > On Thu, 13 Nov 2008 10:58:49 -0800 (PST), [EMAIL PROTECTED] wrote: > >On Nov 13, 11:32 am, "J. Cliff Dyer" <[EMAIL PROTECTED]> wrote: > >> On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote: > > [snip] > >> > Preserving state is wh

Re: using "private" parameters as static storage?

2008-11-13 Thread Jean-Paul Calderone
On Thu, 13 Nov 2008 10:58:49 -0800 (PST), [EMAIL PROTECTED] wrote: On Nov 13, 11:32 am, "J. Cliff Dyer" <[EMAIL PROTECTED]> wrote: On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote: [snip] > Preserving state is what classes are for. Preserving state is what *objects* are for. Not exclusively

Re: using "private" parameters as static storage?

2008-11-13 Thread rurpy
On Nov 13, 11:32 am, "J. Cliff Dyer" <[EMAIL PROTECTED]> wrote: > On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote: > > On Nov 13, 9:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: > > > One thing I miss as I move from REALbasic to Python is the ability to > > > have static storage within a method --

Re: using "private" parameters as static storage?

2008-11-13 Thread J. Cliff Dyer
On Thu, 2008-11-13 at 11:19 -0600, Chris Mellon wrote: > On Thu, Nov 13, 2008 at 11:16 AM, Joe Strout <[EMAIL PROTECTED]> wrote: > > 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, b

Re: using "private" parameters as static storage?

2008-11-13 Thread Steve Holden
Joe Strout wrote: > 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 ma

Re: using "private" parameters as static storage?

2008-11-13 Thread J. Cliff Dyer
On Thu, 2008-11-13 at 09:38 -0800, Matimus wrote: > On Nov 13, 9:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: > > 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 o

Re: using "private" parameters as static storage?

2008-11-13 Thread Matimus
On Nov 13, 9:16 am, Joe Strout <[EMAIL PROTECTED]> wrote: > 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 thi

Re: using "private" parameters as static storage?

2008-11-13 Thread Chris Mellon
On Thu, Nov 13, 2008 at 11:16 AM, Joe Strout <[EMAIL PROTECTED]> wrote: > 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 su

using "private" parameters as static storage?

2008-11-13 Thread Joe Strout
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 facto