Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - inheriting from old-style classes

I am trying to connect via telnet to a laboratory instrument. I'd like to extend the Telnet class from the telnetlib module in the standard library, to include functions specific to our instrument:

import telnetlib class Instrument(telnetlib.Telnet):     def __init__(self, host=None, port=0, timeout=5):         super(Instrument,self).__init__(host, port, timeout) 

All I am trying to do in this code is inherit the __init__ method from the parent class (telnetlib.Telnet) and pass on the standard arguments, so I can add things to __init__ later. This formula has worked for me on other occasions; this time it gives me an error at the super() statement when I try to instantiate:

TypeError: must be type, not classobj 

I looked at the source code for telnetlib, and Telnet appears to be an old-style class (it doesn't inherit from object) - I'm wondering if this could be the source of my problem? If so, how can it be overcome? I've seen some code examples where the derived class inherits both from the superclass and object, though I'm not entirely sure if this is a response to the same problem as me.

Full disclosure: I have also tried using telnetlib.Telnet in place of super(), and from telnetlib import Telnet with Telnet in place of super(). The problem persists in these cases.

Thanks!

like image 618
Benjamin Hodgson Avatar asked Jul 17 '12 17:07

Benjamin Hodgson


People also ask

Can you inherit classes in Python?

Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.

Do all classes in Python inherit from object?

Two built-in functions isinstance() and issubclass() are used to check inheritances. The function isinstance() returns True if the object is an instance of the class or other classes derived from it. Each and every class in Python inherits from the base class object .

Do Python subclasses inherit methods?

Classes called child classes or subclasses inherit methods and variables from parent classes or base classes.

Do subclasses inherit attributes?

A subclass “inherits” all the attributes (methods, etc) of the parent class. This means that a subclass will have everything that its “parents” have. You can then change (“override”) some or all of the attributes to change the behavior.


1 Answers

You need to call the constructor like this:

telnetlib.Telnet.__init__(self, host, port, timeout) 

You need to add the explicit self since telnet.Telnet.__init__ is not a bound method but rather an unbound method, i.e. witout an instance assigned. So when calling it you need to pass the instance explicitely.

>>> Test.__init__ <unbound method Test.__init__> >>> Test().__init__ <bound method Test.__init__ of <__main__.Test instance at 0x7fb54c984e18>> >>> Test.__init__() Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: unbound method __init__() must be called with Test instance as first argument (got nothing instead) 
like image 63
ThiefMaster Avatar answered Oct 14 '22 06:10

ThiefMaster