Search code examples
pythonmaya

Why am I getting an error asking for one argument to be passed with this super init function


When I run this code in the script editor of Maya, I get this error:

TypeError: super() takes at least 1 argument (0 given)

I do not understand what my super init function is requiring.

google, and youtube. I am running this code in maya 2018.

import maya.cmds as cmds

class one:
    mod_txt = "_one_mod"         

    def __init__(self,txt):
        self.txt = txt

    def mod_txt_method(self):
        self.txt = self.txt + self.mod_txt

class two(one):
    mod_txt = "_two_mod" 

    def __init__(self,txt,txt_two):
        super().__init__(self,txt)
        self.txt_two = text_two    

ltv = two('doug','chris')

print ltv.txt
print ltv.txt_two

I would think I should be able to add the new attribute txt_two to my class, two.


Solution

  • There were a few issues with the script.

    First, one needs to subclass something, in this case object, otherwise super will fail.

    Next, for super to access its inherited __init__ you need to pass the class and instance: super(two, self).__init__(txt). No need to pass self to the __init__ method, just the arguments the method requires.

    There's also an issue in two's __init__ method where the variable text_two doesn't exist (probably a typo?).

    Now the script executes as expected. You can also consider cleaning up the script so that it uses standard conventions: Class names should begin with an uppercase, use double spaces to separate blocks of code when they're at module level, and use a space after a comma.

    Here's the final code:

    import maya.cmds as cmds
    
    
    class One(object):
    
        mod_txt = "_one_mod"         
    
        def __init__(self, txt):
            self.txt = txt
    
        def mod_txt_method(self):
            self.txt = self.txt + self.mod_txt
    
    
    class Two(One):
    
        mod_txt = "_two_mod" 
    
        def __init__(self, txt, txt_two):
            super(Two, self).__init__(txt)
            self.txt_two = txt_two    
    
    
    ltv = Two('doug', 'chris')
    
    print ltv.txt
    print ltv.txt_two