[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Can't define __call__ within __init__?

Neal Becker

3/10/2010 1:41:00 PM

Simon Brunning wrote:

> On 10 March 2010 13:12, Neal Becker <ndbecker2@gmail.com> 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):
>> return 1
>>
>>
>> x = X(0)
>>
>> x()
>> TypeError: 'X' object is not callable
>
> __call__ is in the __init__ method's local namespace - you need to
> bind it to the class's namespace instead:
>
> X.__call__ = __call__
>
> But this probably isn't what you want either, since all instances of X
> will share the same method.
>
> What are you trying to do? In your simple example, you'd be much
> better off with a single __call__ method. But you knew that.
>

Sorry, a bit early in the morning. This works:
class X (object):
def __init__(self, i):
if i == 0:
def F (self):
return 0
else:
def F (self):
return 1
self.F = F

def __call__ (self):
return self.F (self)


Not sure if there is a more elegant (or compact) way to write this.
Could __call__ be defined directly within __init__?

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.

6 Answers

Duncan Booth

3/10/2010 5:39:00 PM

0

Neal Becker <ndbecker2@gmail.com> 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 to use different classes.

You can keep all the common behaviour in a base class and just override the
__call__ method for the different behaviour. Then use a factory function to
decide which class to instantiate or else override __new__ and make the
decision there. e.g.

>>> class X(object):
def __call__(self):
return 0
def __new__(cls, i):
if i!=0:
cls = Y
return object.__new__(cls)


>>> class Y(X):
def __call__(self):
return 1


>>> x = X(0)
>>> x()
0
>>> y = X(1)
>>> y()
1
>>> isinstance(x, X)
True
>>> isinstance(y, X)
True

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.

Neal Becker

3/10/2010 6:23:00 PM

0

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 mail list. I don't know what to do about it. I
think the problem only happens on python's mail list (I've never seen it
reported on any of the MANY other lists I use via gmane).

Duncan Booth

3/10/2010 6:38:00 PM

0

Neal Becker <ndbecker2@gmail.com> 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's
> nntp- smtp gateway and python's mail list. I don't know what to do
> about it. I think the problem only happens on python's mail list
> (I've never seen it reported on any of the MANY other lists I use via
> gmane).
>
Are the other mailing lists gatewayed from Usenet? It may not matter if
there's a followup-to header on a mailing list, it probably just gets
ignored, but it does matter on Usenet (which after all is what Gmane is
emulating).

Neal Becker

3/10/2010 7:43:00 PM

0

Duncan Booth wrote:

> Neal Becker <ndbecker2@gmail.com> 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's
>> nntp- smtp gateway and python's mail list. I don't know what to do
>> about it. I think the problem only happens on python's mail list
>> (I've never seen it reported on any of the MANY other lists I use via
>> gmane).
>>
> Are the other mailing lists gatewayed from Usenet? It may not matter if
> there's a followup-to header on a mailing list, it probably just gets
> ignored, but it does matter on Usenet (which after all is what Gmane is
> emulating).

For the record, it isn't really gatewayed to usenet - it's just allowing you
to read your favorite ML via nntp - which is MUCH more sensible than
actually having all that mail delivered personally to you, if you read a lot
of lists.

Robert Kern

3/10/2010 8:09:00 PM

0

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 gmane's nntp-
> smtp gateway and python's mail list. I don't know what to do about it. I
> think the problem only happens on python's mail list (I've never seen it
> reported on any of the MANY other lists I use via gmane).

I think it may be your news reader adding the Original-Followup-To header. I use
Thunderbird 3 to read this list via GMane, too, but my posts do not contain the
header. What newsreader are you using?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Neal Becker

3/10/2010 11:38:00 PM

0

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 that, there is some bad interaction between gmane's nntp-
>> smtp gateway and python's mail list. I don't know what to do about it.
>> I think the problem only happens on python's mail list (I've never seen
>> it reported on any of the MANY other lists I use via gmane).
>
> I think it may be your news reader adding the Original-Followup-To header.
> I use Thunderbird 3 to read this list via GMane, too, but my posts do not
> contain the header. What newsreader are you using?
>

knode.