Re: Can't define __call__ within __init__?

Discussion in 'Python' started by Neal Becker, Mar 10, 2010.

  1. Neal Becker

    Neal Becker Guest

    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_ (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.
    Neal Becker, Mar 10, 2010
    #1
    1. Advertising

  2. Neal Becker

    Neal Becker Guest

    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).
    Neal Becker, Mar 10, 2010
    #2
    1. Advertising

  3. Neal Becker

    Neal Becker Guest

    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 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.
    Neal Becker, Mar 10, 2010
    #3
  4. Neal Becker

    Robert Kern Guest

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


    python-list is also gatewayed to the real USENET group comp.lang.python in
    addition to its GMane gateway. Duncan is reading comp.lang.python from a real
    USENET server, not via python-list through his email client. Most of the other
    lists you read via GMane aren't gatewayed to the real USENET, so your
    Followup-To header never caused a problem for anyone else.

    --
    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
    Robert Kern, Mar 10, 2010
    #4
  5. Neal Becker

    Robert Kern Guest

    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
    Robert Kern, Mar 10, 2010
    #5
  6. Neal Becker

    Neal Becker Guest

    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.
    Neal Becker, Mar 10, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Bethard
    Replies:
    2
    Views:
    445
    Steven Bethard
    Feb 16, 2005
  2. Kent Johnson
    Replies:
    7
    Views:
    886
    Jan Niklas Fingerle
    Feb 12, 2006
  3. Neal Becker

    Can't define __call__ within __init__?

    Neal Becker, Mar 10, 2010, in forum: Python
    Replies:
    9
    Views:
    311
    Steven D'Aprano
    Mar 12, 2010
  4. Ramchandra Apte
    Replies:
    17
    Views:
    326
    Manuel Pégourié-Gonnard
    Sep 30, 2012
  5. Marco
    Replies:
    1
    Views:
    172
    Ian Kelly
    Nov 20, 2012
Loading...

Share This Page