5

Somehow, this works fine in the Maya/Python script editor, but fails when it's inside of my module code. Anyone have any ideas?

class ControlShape(object):
    def __init__(self, *args, **kwargs):
        print 'Inside ControlShape...'

class Cross(ControlShape):
    def __init__(self, *args, **kwargs):
        print 'Entering Cross...'
        super(Cross, self).__init__(*args, **kwargs)
        print 'Leaving Cross...'

x = Cross()

This gives me a TypeError: super(type, obj): obj must be an instance or subtype of type.

jedmao
  • 10,224
  • 11
  • 59
  • 65
  • pasted it in ipython and i didn't get any error, sure there is no other code in your program? – catchmeifyoutry May 11 '10 at 04:25
  • The full back-trace would be helpful. This code also works in CPython 2.6.5. – msw May 11 '10 at 04:35
  • yeah the problem is that it doesn't work within a maya module, which i don't understand. – jedmao May 11 '10 at 04:36
  • The error message implies that `self` is not an instance of `Cross`. Could something have rebound `Cross`? Maybe there's something going on in your code or Maya which replaces the name bound to the class with some sort of wrapper? The full backtrace would help: if `Cross` really is some kind of wrapper round the actual class then it should show up in the stack. – Duncan May 11 '10 at 08:31

4 Answers4

22

It has to do with reloading modules. Reloading a module often changes the internal object in memory which makes the isinstance test of super return False.

http://thingspython.wordpress.com/2010/09/27/another-super-wrinkle-raising-typeerror/

Chad Vernon
  • 593
  • 4
  • 12
  • 2
    you just saved my day. My code was used in a deamon that needs a high uptime, and reload of modules is used, which I didn't knew before I asked thanks to your answer. Obviously we'll need something more reliable than this poor hack. – vincent Mar 30 '12 at 09:02
  • 2
    This happens to me fairly often using the autoreload feature of ipython – Zach Dwiel Nov 21 '13 at 02:54
4

I had this exact same problem. It's definitely not practical to restart maya each time you make a change. I found an answer here that solved this problem for me.

You should read the linked answer to understand why its only suitable for debugging. But briefly, put this code in userSetup.py, then each time you edit your code run reload_package(my_package)

import sys, types
def reload_package(root_module):
    package_name = root_module.__name__

    # get a reference to each loaded module
    loaded_package_modules = dict([
        (key, value) for key, value in sys.modules.items() 
        if key.startswith(package_name) and isinstance(value, types.ModuleType)])

    # delete references to these loaded modules from sys.modules
    for key in loaded_package_modules:
        del sys.modules[key]

    # load each of the modules again; 
    # make old modules share state with new modules
    for key in loaded_package_modules:
        print 'loading %s' % key
        newmodule = __import__(key)
        oldmodule = loaded_package_modules[key]
        oldmodule.__dict__.clear()
        oldmodule.__dict__.update(newmodule.__dict__)
Community
  • 1
  • 1
Julian Mann
  • 6,256
  • 5
  • 31
  • 43
  • 1
    is there anyway to hook ipython's autoreload to this? otherwise have to restart kernel any time a line is changed in a package with `super` – dashesy Sep 03 '14 at 18:40
  • this works even when `%autoreload 2` in ipython does not work, throwing `TypeError: super(type, obj): obj must be an instance or subtype of type` ` – dashesy Sep 03 '14 at 19:41
0

It is good rule of thumb if you're using the super(Class, self).__init__ that you ALWAYS call it this way. This applies to your classes that inherit from object.

class ControlShape(object):
   def __init__(self, *args, **kwargs):
      super(ControlShape, self).__init__()
      print 'Inside ControlShape...'

See if that fixes your error. Just a guess as I don't use maya, but worth a shot.

manifest
  • 2,208
  • 16
  • 13
0

Turns out it had something to do with my imports at the top of the module. I forget which one it was, though. I should have posted this the moment I discovered what it was.

jedmao
  • 10,224
  • 11
  • 59
  • 65