Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python super() behavior not dependable

For some reason, the super() method is not always behaving as expected, opting to return:

TypeError('super(type, obj): obj must be an instance or subtype of type)' 

I understand what the error means. I do not understand why it is coming up as an error. Here's the snippet of code that is breaking. All objects in the system are new style objects.

What's really interesting is that this error does not always show up. I don't know what's causing it. The super() method in Retrieval is passing the Retrieval class, and then itself as an object, which is, as far as I'm aware,exactly how super() is supposed to be invoked.

Any thoughts at all?

In file DBConnection.py:

class DBAdminConnection(object):     def __init__(self):         self.user = DBUserConnection().user          self.submissions = DBSubmissionConnection() 

In file Retrieval.py

class Retrieval(DBConnection.DBAdminConnection):      def __init__(self, username=None, password=None, unique_key=None):         super(Retrieval,self).__init__()         if username and password:             self.username = username             self.user.login(username,password, config.DATABASE)             if self.user.error:                 raise UserLoginError(username)         self.unique_key = unique_key 
like image 271
Thomas Thorogood Avatar asked Mar 15 '12 14:03

Thomas Thorogood


People also ask

What does super () do Python?

The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.

Should I use super in Python?

Yes, you should use super() over other methods. This is now the standard object inheritance model in Python 3.

What does super () __ Init__ do in Python?

When you initialize a child class in Python, you can call the super(). __init__() method. This initializes the parent class object into the child class. In addition to this, you can add child-specific information to the child object as well.

When and why do we use super () Python?

Use of super() In Python, super() has two major use cases: Allows us to avoid using the base class name explicitly. Working with Multiple Inheritance.


1 Answers

Are you reloading modules somehow in the middle of things? If so, that may explain this error.

isinstance(self,DBAdminConnection) may become false after reloading modules because of the changes to memory references, apparently.

Edit: if you're running your web.py app under mod_wsgi, make sure you're disabling autoreload:

app = web.application(urls, globals(), autoreload=False) 
like image 167
Eduardo Ivanec Avatar answered Sep 22 '22 06:09

Eduardo Ivanec