Problem with sub-classing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bernard Lebel

    #1

    Problem with sub-classing

    Hello,

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

    Traceback (most recent call last):

    File "<Script Block >", line 344, in BBExportElement Granules_Execut e
    from bb_pipeline import bb_exportelemen tgranules

    File "\\Linuxserver\ ANIMATION\XSI\W ORKGROUP_ANIMAT ION\Data\Script s\bb_pipeline\b b_exportelement granules.py",
    line 101, in ?
    oWriter = bbExportGranule s()

    File "\\Linuxserver\ ANIMATION\XSI\W ORKGROUP_ANIMAT ION\Data\Script s\bb_pipeline\b b_granules\bb_g ranuleexport\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 bbExportGranule s instance instead)
    - [line 343 in
    \\Linuxserver\A NIMATION\XSI\WO RKGROUP_ANIMATI ON\plugins\bb_p ipeline\bb_pipe line_py.py]


    In normal English:

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


    The init of bbExportGranule s:

    class bbExportGranule s( bbExportMeta, bbExportCluster s,
    bbExportMateria ls, bbExportKinemat ics, bbExportModels, bbExportMetaToc
    ):

    def __init__( self ):

    bbExportMeta.__ init__( self )
    bbExportCluster s.__init__( self )
    bbExportMateria ls.__init__( self )
    bbExportKinemat ics.__init__( self )
    bbExportModels. __init__( self )


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

    class bbExportMeta:

    def __init__( self ):

    self.iPreviousA ssetVersion = gec.iNOPREVIOUS ASSETVERSION
    self.iCurrentAs setVersion = gec.iMINCURRENT ASSETVERSION
    self.sPreviousA ssetProject = xsi.getdefaultp roject()




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



    Thanks
    Bernard
  • Peter Otten

    #2
    Re: Problem with sub-classing

    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 BBExportElement Granules_Execut e
    from bb_pipeline import bb_exportelemen tgranules
    >
    File
    >
    "\\Linuxserver\ ANIMATION\XSI\W ORKGROUP_ANIMAT ION\Data\Script s\bb_pipeline\b b_exportelement granules.py",
    line 101, in ?
    oWriter = bbExportGranule s()
    >
    File
    >
    "\\Linuxserver\ ANIMATION\XSI\W ORKGROUP_ANIMAT ION\Data\Script s\bb_pipeline\b b_granules\bb_g ranuleexport\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 bbExportGranule s instance instead)
    - [line 343 in
    >
    \\Linuxserver\A NIMATION\XSI\WO RKGROUP_ANIMATI ON\plugins\bb_p ipeline\bb_pipe line_py.py]
    >
    >
    In normal English:
    >
    I have class bbExportGranule s
    bbExportGranule s is a sub-class of 6 other classes
    In the __init__() of bbExportGranule s class, I call the __init__() of
    5 of these super-classes.
    >
    >
    The init of bbExportGranule s:
    >
    class bbExportGranule s( bbExportMeta, bbExportCluster s,
    bbExportMateria ls, bbExportKinemat ics, bbExportModels, bbExportMetaToc
    ):
    >
    def __init__( self ):
    >
    bbExportMeta.__ init__( self )
    bbExportCluster s.__init__( self )
    bbExportMateria ls.__init__( self )
    bbExportKinemat ics.__init__( self )
    bbExportModels. __init__( self )
    >
    >
    And the bbExportMeta class (the error is raised when bbExportGranule s
    is subclassing that one):
    >
    class bbExportMeta:
    >
    def __init__( self ):
    >
    self.iPreviousA ssetVersion = gec.iNOPREVIOUS ASSETVERSION
    self.iCurrentAs setVersion = gec.iMINCURRENT ASSETVERSION
    self.sPreviousA ssetProject = xsi.getdefaultp roject()
    >
    >
    >
    >
    Any suggestion? I really, really don't see what I'm doing wrong here,
    especially that it actually used to work!
    Change bbExportGranule s to

    class bbExportGranule s( bbExportMeta, bbExportCluster s,
    bbExportMateria ls, bbExportKinemat ics, bbExportModels, bbExportMetaToc
    ):
            
            def __init__(self, bbExportMeta=bb ExportMeta): # the only change
                    
                    bbExportMeta.__ init__( self )
                    bbExportCluster s.__init__( self )
                    bbExportMateria ls.__init__( self )
                    bbExportKinemat ics.__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

    Comment

    • Bernard Lebel

      #3
      Re: Problem with sub-classing

      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 <__peter__@web. dewrote:
      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 BBExportElement Granules_Execut e
      from bb_pipeline import bb_exportelemen tgranules

      File
      "\\Linuxserver\ ANIMATION\XSI\W ORKGROUP_ANIMAT ION\Data\Script s\bb_pipeline\b b_exportelement granules.py",
      line 101, in ?
      oWriter = bbExportGranule s()

      File
      "\\Linuxserver\ ANIMATION\XSI\W ORKGROUP_ANIMAT ION\Data\Script s\bb_pipeline\b b_granules\bb_g ranuleexport\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 bbExportGranule s instance instead)
      - [line 343 in
      \\Linuxserver\A NIMATION\XSI\WO RKGROUP_ANIMATI ON\plugins\bb_p ipeline\bb_pipe line_py.py]


      In normal English:

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


      The init of bbExportGranule s:

      class bbExportGranule s( bbExportMeta, bbExportCluster s,
      bbExportMateria ls, bbExportKinemat ics, bbExportModels, bbExportMetaToc
      ):

      def __init__( self ):

      bbExportMeta.__ init__( self )
      bbExportCluster s.__init__( self )
      bbExportMateria ls.__init__( self )
      bbExportKinemat ics.__init__( self )
      bbExportModels. __init__( self )


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

      class bbExportMeta:

      def __init__( self ):

      self.iPreviousA ssetVersion = gec.iNOPREVIOUS ASSETVERSION
      self.iCurrentAs setVersion = gec.iMINCURRENT ASSETVERSION
      self.sPreviousA ssetProject = xsi.getdefaultp roject()




      Any suggestion? I really, really don't see what I'm doing wrong here,
      especially that it actually used to work!
      >
      Change bbExportGranule s to
      >
      class bbExportGranule s( bbExportMeta, bbExportCluster s,
      bbExportMateria ls, bbExportKinemat ics, bbExportModels, bbExportMetaToc
      ):
      >
      def __init__(self, bbExportMeta=bb ExportMeta): # the only change
      >
      bbExportMeta.__ init__( self )
      bbExportCluster s.__init__( self )
      bbExportMateria ls.__init__( self )
      bbExportKinemat ics.__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
      >
      --

      >

      Comment

      • Peter Otten

        #4
        Re: Problem with sub-classing

        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



        Comment

        Working...