0

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.

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
winteralfs
  • 459
  • 6
  • 25
  • 2
    `super()` without explicit parameters has been introduced in Python 3, Maya uses Python 2.7 - you need to call `super(one, self)` instead – UnholySheep Jan 24 '19 at 21:25
  • changing the format to work with Python 2.7, i.e. super(one,self).__init__(self,txt). I am now getting the error:TypeError: must be type, not classobj – winteralfs Jan 24 '19 at 21:31

1 Answers1

1

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
Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • Much appreciated. – winteralfs Jan 25 '19 at 04:27
  • Don't forget to accept this if it fixed your issue otherwise others will think it's not resolved. – Green Cell Jan 25 '19 at 05:34
  • how does the super know to inherit One’s init if Two is called in its own init (self), and Two is also called in the super ( Two, Self )? Where is One’s init being passed into Two? – winteralfs Jan 25 '19 at 05:44
  • 1
    Since, `Two` is inheriting from `One` it calls its `__init__`. I would just be regurgitating from others who already have done a great job explaining, so check these out too: https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods https://www.pythonforbeginners.com/super/working-python-super-function – Green Cell Jan 25 '19 at 06:09