Problem with sub-classing

Discussion in 'Python' started by Bernard Lebel, Jul 17, 2006.

  1. Hello,

    I have this problem when subclassing classes where I get this error:

    Traceback (most recent call last):

    File "<Script Block >", line 344, in BBExportElementGranules_Execute
    from bb_pipeline import bb_exportelementgranules

    File "\\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\Data\Scripts\bb_pipeline\bb_exportelementgranules.py",
    line 101, in ?
    oWriter = bbExportGranules()

    File "\\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\Data\Scripts\bb_pipeline\bb_granules\bb_granuleexport\bb_granuleexport_element.py",
    line 73, in __init__
    bbExportMeta.__init__( self )

    TypeError: unbound method __init__() must be called with bbExportMeta
    instance as first argument (got bbExportGranules instance instead)
    - [line 343 in
    \\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\plugins\bb_pipeline\bb_pipeline_py.py]


    In normal English:

    I have class bbExportGranules
    bbExportGranules is a sub-class of 6 other classes
    In the __init__() of bbExportGranules class, I call the __init__() of
    5 of these super-classes.


    The init of bbExportGranules:

    class bbExportGranules( bbExportMeta, bbExportClusters,
    bbExportMaterials, bbExportKinematics, bbExportModels, bbExportMetaToc
    ):

    def __init__( self ):

    bbExportMeta.__init__( self )
    bbExportClusters.__init__( self )
    bbExportMaterials.__init__( self )
    bbExportKinematics.__init__( self )
    bbExportModels.__init__( self )


    And the bbExportMeta class (the error is raised when bbExportGranules
    is subclassing that one):

    class bbExportMeta:

    def __init__( self ):

    self.iPreviousAssetVersion = gec.iNOPREVIOUSASSETVERSION
    self.iCurrentAssetVersion = gec.iMINCURRENTASSETVERSION
    self.sPreviousAssetProject = xsi.getdefaultproject()




    Any suggestion? I really, really don't see what I'm doing wrong here,
    especially that it actually used to work!



    Thanks
    Bernard
     
    Bernard Lebel, Jul 17, 2006
    #1
    1. Advertising

  2. Bernard Lebel

    Peter Otten Guest

    Bernard Lebel wrote:

    > Hello,
    >
    > I have this problem when subclassing classes where I get this error:
    >
    > Traceback (most recent call last):
    >
    > File "<Script Block >", line 344, in BBExportElementGranules_Execute
    > from bb_pipeline import bb_exportelementgranules
    >
    > File
    >

    "\\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\Data\Scripts\bb_pipeline\bb_exportelementgranules.py",
    > line 101, in ?
    > oWriter = bbExportGranules()
    >
    > File
    >

    "\\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\Data\Scripts\bb_pipeline\bb_granules\bb_granuleexport\bb_granuleexport_element.py",
    > line 73, in __init__
    > bbExportMeta.__init__( self )
    >
    > TypeError: unbound method __init__() must be called with bbExportMeta
    > instance as first argument (got bbExportGranules instance instead)
    > - [line 343 in
    >

    \\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\plugins\bb_pipeline\bb_pipeline_py.py]
    >
    >
    > In normal English:
    >
    > I have class bbExportGranules
    > bbExportGranules is a sub-class of 6 other classes
    > In the __init__() of bbExportGranules class, I call the __init__() of
    > 5 of these super-classes.
    >
    >
    > The init of bbExportGranules:
    >
    > class bbExportGranules( bbExportMeta, bbExportClusters,
    > bbExportMaterials, bbExportKinematics, bbExportModels, bbExportMetaToc
    > ):
    >
    > def __init__( self ):
    >
    > bbExportMeta.__init__( self )
    > bbExportClusters.__init__( self )
    > bbExportMaterials.__init__( self )
    > bbExportKinematics.__init__( self )
    > bbExportModels.__init__( self )
    >
    >
    > And the bbExportMeta class (the error is raised when bbExportGranules
    > is subclassing that one):
    >
    > class bbExportMeta:
    >
    > def __init__( self ):
    >
    > self.iPreviousAssetVersion = gec.iNOPREVIOUSASSETVERSION
    > self.iCurrentAssetVersion = gec.iMINCURRENTASSETVERSION
    > self.sPreviousAssetProject = xsi.getdefaultproject()
    >
    >
    >
    >
    > Any suggestion? I really, really don't see what I'm doing wrong here,
    > especially that it actually used to work!


    Change bbExportGranules to

    class bbExportGranules( bbExportMeta, bbExportClusters,
    bbExportMaterials, bbExportKinematics, bbExportModels, bbExportMetaToc
    ):
            
            def __init__(self, bbExportMeta=bbExportMeta): # the only change
                    
                    bbExportMeta.__init__( self )
                    bbExportClusters.__init__( self )
                    bbExportMaterials.__init__( self )
                    bbExportKinematics.__init__( self )
                    bbExportModels.__init__( self )

    If it then starts to work again you are probably rebinding bbExportMeta
    later in your script, e. g:

    >>> class A:

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

    .... def method(self):
    .... A.method(self)
    ....
    >>> b = B()
    >>> b.method() # no error
    >>> class A: # rebinding A

    .... def method(self): pass
    ....
    >>> b.method() # oops

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

    One way to make that happen is to import the same file twice, once as part
    of a package and once directly. Solution: never set a path into a
    package...

    Peter
     
    Peter Otten, Jul 17, 2006
    #2
    1. Advertising

  3. Okay, that make sense.

    Now the question is: regarding the re-binding behavior, is this
    actually problematic? By that I mean that is it good coding practice
    to avoid this issue altogether as much as possible, or is it okay to
    live with it if you use the __init__ argument trick you have shown?


    Thanks
    Bernard



    On 7/17/06, Peter Otten <> wrote:
    > Bernard Lebel wrote:
    >
    > > Hello,
    > >
    > > I have this problem when subclassing classes where I get this error:
    > >
    > > Traceback (most recent call last):
    > >
    > > File "<Script Block >", line 344, in BBExportElementGranules_Execute
    > > from bb_pipeline import bb_exportelementgranules
    > >
    > > File
    > >

    > "\\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\Data\Scripts\bb_pipeline\bb_exportelementgranules.py",
    > > line 101, in ?
    > > oWriter = bbExportGranules()
    > >
    > > File
    > >

    > "\\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\Data\Scripts\bb_pipeline\bb_granules\bb_granuleexport\bb_granuleexport_element.py",
    > > line 73, in __init__
    > > bbExportMeta.__init__( self )
    > >
    > > TypeError: unbound method __init__() must be called with bbExportMeta
    > > instance as first argument (got bbExportGranules instance instead)
    > > - [line 343 in
    > >

    > \\Linuxserver\ANIMATION\XSI\WORKGROUP_ANIMATION\plugins\bb_pipeline\bb_pipeline_py.py]
    > >
    > >
    > > In normal English:
    > >
    > > I have class bbExportGranules
    > > bbExportGranules is a sub-class of 6 other classes
    > > In the __init__() of bbExportGranules class, I call the __init__() of
    > > 5 of these super-classes.
    > >
    > >
    > > The init of bbExportGranules:
    > >
    > > class bbExportGranules( bbExportMeta, bbExportClusters,
    > > bbExportMaterials, bbExportKinematics, bbExportModels, bbExportMetaToc
    > > ):
    > >
    > > def __init__( self ):
    > >
    > > bbExportMeta.__init__( self )
    > > bbExportClusters.__init__( self )
    > > bbExportMaterials.__init__( self )
    > > bbExportKinematics.__init__( self )
    > > bbExportModels.__init__( self )
    > >
    > >
    > > And the bbExportMeta class (the error is raised when bbExportGranules
    > > is subclassing that one):
    > >
    > > class bbExportMeta:
    > >
    > > def __init__( self ):
    > >
    > > self.iPreviousAssetVersion = gec.iNOPREVIOUSASSETVERSION
    > > self.iCurrentAssetVersion = gec.iMINCURRENTASSETVERSION
    > > self.sPreviousAssetProject = xsi.getdefaultproject()
    > >
    > >
    > >
    > >
    > > Any suggestion? I really, really don't see what I'm doing wrong here,
    > > especially that it actually used to work!

    >
    > Change bbExportGranules to
    >
    > class bbExportGranules( bbExportMeta, bbExportClusters,
    > bbExportMaterials, bbExportKinematics, bbExportModels, bbExportMetaToc
    > ):
    >
    > def __init__(self, bbExportMeta=bbExportMeta): # the only change
    >
    > bbExportMeta.__init__( self )
    > bbExportClusters.__init__( self )
    > bbExportMaterials.__init__( self )
    > bbExportKinematics.__init__( self )
    > bbExportModels.__init__( self )
    >
    > If it then starts to work again you are probably rebinding bbExportMeta
    > later in your script, e. g:
    >
    > >>> class A:

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

    > ... def method(self):
    > ... A.method(self)
    > ...
    > >>> b = B()
    > >>> b.method() # no error
    > >>> class A: # rebinding A

    > ... def method(self): pass
    > ...
    > >>> b.method() # oops

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "<stdin>", line 3, in method
    > TypeError: unbound method method() must be called with A instance as first
    > argument (got B instance instead)
    >
    > One way to make that happen is to import the same file twice, once as part
    > of a package and once directly. Solution: never set a path into a
    > package...
    >
    > Peter
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Bernard Lebel, Jul 17, 2006
    #3
  4. Bernard Lebel

    Peter Otten Guest

    Bernard Lebel wrote:

    > Okay, that make sense.
    >
    > Now the question is: regarding the re-binding behavior, is this
    > actually problematic? By that I mean that is it good coding practice
    > to avoid this issue altogether as much as possible, or is it okay to
    > live with it if you use the __init__ argument trick you have shown?


    I suggested the "argument trick" for diagnosis only.

    One /good/ coding practice is to choose descriptive names for (toplevel)
    objects. Another is to avoid

    from module import *

    style imports which tend to be the most common source of name clashes.


    Peter
     
    Peter Otten, Jul 17, 2006
    #4
    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. Pritam Kamat

    Obfuscation and sub classing

    Pritam Kamat, Aug 8, 2003, in forum: Java
    Replies:
    2
    Views:
    533
    pete kirkham
    Aug 8, 2003
  2. Colin J. Williams

    Sub-classing NumArray - two questions

    Colin J. Williams, Oct 1, 2003, in forum: Python
    Replies:
    0
    Views:
    281
    Colin J. Williams
    Oct 1, 2003
  3. Colin J. Williams
    Replies:
    1
    Views:
    277
    Michael Amrhein
    Aug 15, 2007
  4. Colin J. Williams
    Replies:
    5
    Views:
    334
    Colin J. Williams
    Aug 17, 2007
  5. Torsten Bronger
    Replies:
    9
    Views:
    270
    Martin v. Löwis
    Dec 31, 2007
Loading...

Share This Page