problem calling parent's __init__ method

Discussion in 'Python' started by Ryan Krauss, Aug 7, 2008.

  1. Ryan Krauss

    Ryan Krauss Guest

    I am trying to call a parent's __init__ method from the child's:

    class ArbitraryBlock(InnerBlock):
    def __init__(self, codelist, noout=False, **kwargs):
    InnerBlock.__init__(self, codelist, noout=noout, **kwargs)


    I get this error:

    <type 'exceptions.TypeError'>: unbound method __init__() must be
    called with InnerBlock instance as first argument (got ArbitraryBlock
    instance instead)


    I found a thread that talked about the parent and child being
    different types, so I tried setting up the parent class 3 different
    ways:

    class InnerBlock:

    class InnerBlock(object):

    class InnerBlock(empty_class):

    where

    class empty_class(object):
    def __init__(self,_d={},**kwargs):
    kwargs.update(_d)
    self.__dict__=kwargs

    I still get the same error. Why doesn't this work?

    Thanks,

    Ryan
    Ryan Krauss, Aug 7, 2008
    #1
    1. Advertising

  2. Ryan Krauss

    Peter Otten Guest

    Ryan Krauss wrote:

    > I am trying to call a parent's __init__ method from the child's:
    >
    > class ArbitraryBlock(InnerBlock):
    > def __init__(self, codelist, noout=False, **kwargs):
    > InnerBlock.__init__(self, codelist, noout=noout, **kwargs)
    >
    >
    > I get this error:
    >
    > <type 'exceptions.TypeError'>: unbound method __init__() must be
    > called with InnerBlock instance as first argument (got ArbitraryBlock
    > instance instead)
    >
    >
    > I found a thread that talked about the parent and child being
    > different types, so I tried setting up the parent class 3 different
    > ways:
    >
    > class InnerBlock:
    >
    > class InnerBlock(object):
    >
    > class InnerBlock(empty_class):
    >
    > where
    >
    > class empty_class(object):
    > def __init__(self,_d={},**kwargs):
    > kwargs.update(_d)
    > self.__dict__=kwargs
    >
    > I still get the same error. Why doesn't this work?


    For some reason you have two InnerBlock classes in your code. A
    demonstration:

    >>> class A(object):

    .... def __init__(self): pass
    ....
    >>> class B(A):

    .... def __init__(self):
    .... A.__init__(self)
    ....
    >>> B()

    <__main__.B object at 0x2b67aae02c90>
    >>> class A(object):

    .... def __init__(self): pass
    ....
    >>> B()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 3, in __init__
    TypeError: unbound method __init__() must be called with A instance as first
    argument (got B instance instead)

    Are you perhaps importing your main script into your main script?

    Peter
    Peter Otten, Aug 7, 2008
    #2
    1. Advertising

  3. Ryan Krauss

    Ryan Krauss Guest

    It is in fact an import problem. My module works fine by itself with
    a test case in if __name__ == '__main__', but I have a problem when
    using it within another module when both are underdevelopment.
    Changing the reload order solves the problem.

    This is bad:
    import texpy, os, pytex
    reload(texpy)
    reload(pytex)

    This is fine:
    import texpy, os, pytex
    reload(pytex)
    reload(texpy)

    The module we have been talking about is pytex. It gets imported into
    texpy. The reload statements are the problem (I am fairly certain),
    and I am only using them because things are under development.

    Thanks to Peter and Jerry for their help.

    Ryan


    On Thu, Aug 7, 2008 at 10:36 AM, Peter Otten <> wrote:
    > Ryan Krauss wrote:
    >
    >> I am trying to call a parent's __init__ method from the child's:
    >>
    >> class ArbitraryBlock(InnerBlock):
    >> def __init__(self, codelist, noout=False, **kwargs):
    >> InnerBlock.__init__(self, codelist, noout=noout, **kwargs)
    >>
    >>
    >> I get this error:
    >>
    >> <type 'exceptions.TypeError'>: unbound method __init__() must be
    >> called with InnerBlock instance as first argument (got ArbitraryBlock
    >> instance instead)
    >>
    >>
    >> I found a thread that talked about the parent and child being
    >> different types, so I tried setting up the parent class 3 different
    >> ways:
    >>
    >> class InnerBlock:
    >>
    >> class InnerBlock(object):
    >>
    >> class InnerBlock(empty_class):
    >>
    >> where
    >>
    >> class empty_class(object):
    >> def __init__(self,_d={},**kwargs):
    >> kwargs.update(_d)
    >> self.__dict__=kwargs
    >>
    >> I still get the same error. Why doesn't this work?

    >
    > For some reason you have two InnerBlock classes in your code. A
    > demonstration:
    >
    >>>> class A(object):

    > ... def __init__(self): pass
    > ...
    >>>> class B(A):

    > ... def __init__(self):
    > ... A.__init__(self)
    > ...
    >>>> B()

    > <__main__.B object at 0x2b67aae02c90>
    >>>> class A(object):

    > ... def __init__(self): pass
    > ...
    >>>> B()

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "<stdin>", line 3, in __init__
    > TypeError: unbound method __init__() must be called with A instance as first
    > argument (got B instance instead)
    >
    > Are you perhaps importing your main script into your main script?
    >
    > Peter
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Ryan Krauss, Aug 7, 2008
    #3
    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:
    884
    Jan Niklas Fingerle
    Feb 12, 2006
  3. Rob Cowie
    Replies:
    6
    Views:
    325
    bruno at modulix
    Jun 20, 2006
  4. 7stud
    Replies:
    3
    Views:
    306
    Evan Klitzke
    Aug 18, 2007
  5. Ramchandra Apte
    Replies:
    17
    Views:
    325
    Manuel Pégourié-Gonnard
    Sep 30, 2012
Loading...

Share This Page