Re: Can't define __call__ within __init__?

2010-03-11 Thread Steven D'Aprano
On Thu, 11 Mar 2010 07:56:59 -0500, Neal Becker wrote: > The example I showed was just a toy problem. The real problem is I > expect to call a function many times, and I want to avoid the overhead > of the 'if blah' everytime. Unless the __call__ methods are very small, the overhead of one extra

Re: Can't define __call__ within __init__?

2010-03-11 Thread Steven D'Aprano
On Thu, 11 Mar 2010 08:20:14 -0800, Steve Howell wrote: >> (2) special methods like __call__ are only called on the class, not the >> instance, so you can't give each instance its own method. >> >> > Are you sure about that? This program prints 1, 2, 1, 2. The rules for classic classes are diffe

Re: Can't define __call__ within __init__?

2010-03-11 Thread Peter Otten
Steve Howell wrote: > On Mar 10, 7:18 pm, Steven D'Aprano > wrote: >> (2) special methods like __call__ are only called on the class, not the >> instance, so you can't give each instance its own method. > Are you sure about that? This program prints 1, 2, 1, 2. You are using a classic class

Re: Can't define __call__ within __init__?

2010-03-11 Thread Steve Howell
On Mar 10, 7:18 pm, Steven D'Aprano wrote: > On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote: > > Want to switch __call__ behavior.  Why doesn't this work?  What is the > > correct way to write this? > > > class X (object): > >     def __init__(self, i): > >         if i == 0: > >          

Re: Can't define __call__ within __init__?

2010-03-11 Thread MRAB
Andre Engels wrote: On Thu, Mar 11, 2010 at 2:30 PM, Steve Holden wrote: The example I showed was just a toy problem. The real problem is I expect to call a function many times, and I want to avoid the overhead of the 'if blah' everytime. This is a premature optimization. First, make it wor

Re: Can't define __call__ within __init__?

2010-03-11 Thread Andre Engels
On Thu, Mar 11, 2010 at 2:30 PM, Steve Holden wrote: >> The example I showed was just a toy problem.  The real problem is >> I expect to call a function many times, and I want to avoid the overhead of >> the 'if blah' everytime. >> > This is a premature optimization. First, make it work. Then (if

Re: Can't define __call__ within __init__?

2010-03-11 Thread Steve Holden
Neal Becker wrote: > Steven D'Aprano wrote: > >> On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote: >> >>> Want to switch __call__ behavior. Why doesn't this work? What is the >>> correct way to write this? >>> >>> class X (object): >>> def __init__(self, i): >>> if i == 0: >>>

Re: Can't define __call__ within __init__?

2010-03-11 Thread Neal Becker
Steven D'Aprano wrote: > On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote: > >> Want to switch __call__ behavior. Why doesn't this work? What is the >> correct way to write this? >> >> class X (object): >> def __init__(self, i): >> if i == 0: >> def __call__ (self)

Re: Can't define __call__ within __init__?

2010-03-10 Thread Steven D'Aprano
On Wed, 10 Mar 2010 08:12:14 -0500, Neal Becker wrote: > Want to switch __call__ behavior. Why doesn't this work? What is the > correct way to write this? > > class X (object): > def __init__(self, i): > if i == 0: > def __call__ (self): > return 0 >

Re: Can't define __call__ within __init__?

2010-03-10 Thread Neal Becker
Robert Kern wrote: > On 2010-03-10 12:23 PM, Neal Becker wrote: >> Duncan Booth wrote: >> ... >>> >>> P.S. I don't know what you did in your post but your Followup-To header >>> is pointing to a group on gmane which makes extra work for me replying. >>> Please don't do that. >> >> I'm sorry about

Re: Can't define __call__ within __init__?

2010-03-10 Thread Robert Kern
On 2010-03-10 12:23 PM, Neal Becker wrote: Duncan Booth wrote: ... P.S. I don't know what you did in your post but your Followup-To header is pointing to a group on gmane which makes extra work for me replying. Please don't do that. I'm sorry about that, there is some bad interaction between

Re: Can't define __call__ within __init__?

2010-03-10 Thread Robert Kern
On 2010-03-10 13:42 PM, Neal Becker wrote: Duncan Booth wrote: Neal Becker wrote: Duncan Booth wrote: ... P.S. I don't know what you did in your post but your Followup-To header is pointing to a group on gmane which makes extra work for me replying. Please don't do that. I'm sorry about

Re: Can't define __call__ within __init__?

2010-03-10 Thread Neal Becker
Duncan Booth wrote: > Neal Becker wrote: > >> Duncan Booth wrote: >> ... >>> >>> P.S. I don't know what you did in your post but your Followup-To >>> header is pointing to a group on gmane which makes extra work for me >>> replying. Please don't do that. >> >> I'm sorry about that, there is so

Re: Can't define __call__ within __init__?

2010-03-10 Thread Duncan Booth
Neal Becker wrote: > Duncan Booth wrote: > ... >> >> P.S. I don't know what you did in your post but your Followup-To >> header is pointing to a group on gmane which makes extra work for me >> replying. Please don't do that. > > I'm sorry about that, there is some bad interaction between gmane'

Re: Can't define __call__ within __init__?

2010-03-10 Thread Neal Becker
Duncan Booth wrote: ... > > P.S. I don't know what you did in your post but your Followup-To header is > pointing to a group on gmane which makes extra work for me replying. > Please don't do that. I'm sorry about that, there is some bad interaction between gmane's nntp- smtp gateway and python's

Re: Can't define __call__ within __init__?

2010-03-10 Thread Duncan Booth
Neal Becker wrote: > What I'm trying to do is make a callable whose behavior is switched > based on some criteria that will be fixed for all calls. In my > example, this will ultimately be determined by the setting of a > command line switch. > If you want different behaviour its usually best

Re: Can't define __call__ within __init__?

2010-03-10 Thread Matt Nordhoff
Neal Becker wrote: > Simon Brunning wrote: > >> On 10 March 2010 13:12, Neal Becker wrote: >>> Want to switch __call__ behavior. Why doesn't this work? What is the >>> correct way to write this? >>> >>> class X (object): >>> def __init__(self, i): >>> if i == 0: >>> def __call__ (self): >>> ret

Re: Can't define __call__ within __init__?

2010-03-10 Thread Neal Becker
Simon Brunning wrote: > On 10 March 2010 13:12, Neal Becker wrote: >> Want to switch __call__ behavior. Why doesn't this work? What is the >> correct way to write this? >> >> class X (object): >> def __init__(self, i): >> if i == 0: >> def __call__ (self): >> return 0 >> else: >> def __call_ (s

Re: Can't define __call__ within __init__?

2010-03-10 Thread Mark Lawrence
Neal Becker wrote: Want to switch __call__ behavior. Why doesn't this work? What is the correct way to write this? class X (object): def __init__(self, i): if i == 0: def __call__ (self): return 0 else: def __call_ (self):

Re: Can't define __call__ within __init__?

2010-03-10 Thread Simon Brunning
On 10 March 2010 13:12, Neal Becker wrote: > Want to switch __call__ behavior.  Why doesn't this work?  What is the > correct way to write this? > > class X (object): >    def __init__(self, i): >        if i == 0: >            def __call__ (self): >                return 0 >        else: >      

Can't define __call__ within __init__?

2010-03-10 Thread Neal Becker
Want to switch __call__ behavior. Why doesn't this work? What is the correct way to write this? class X (object): def __init__(self, i): if i == 0: def __call__ (self): return 0 else: def __call_ (self): return 1 x =