简体   繁体   English

Python - 继承旧式类

[英]Python - inheriting from old-style classes

I am trying to connect via telnet to a laboratory instrument. 我试图通过telnet连接到实验室仪器。 I'd like to extend the Telnet class from the telnetlib module in the standard library, to include functions specific to our instrument: 我想从标准库中的telnetlib模块扩展Telnet类,以包含特定于我们仪器的函数:

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. 我在这段代码中尝试做的就是从父类( telnetlib.Telnet )继承__init__方法并传递标准参数,所以我可以稍后向__init__添加内容。 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: 这次当我尝试实例化时,它在super()语句中给出了一个错误:

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? 我查看了telnetlib的源代码,Telnet似乎是一个旧式类(它不是从object继承) - 我想知道这是否可能是我的问题的根源? 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. 我已经看到了一些代码示例,其中派生类继承了超类和object ,但我不完全确定这是否是对我的同一问题的响应。

Full disclosure: I have also tried using telnetlib.Telnet in place of super() , and from telnetlib import Telnet with Telnet in place of super() . 完全披露:我也尝试使用telnetlib.Telnet代替super() ,并from telnetlib import TelnetTelnet代替super() The problem persists in these cases. 在这些情况下问题仍然存在。

Thanks! 谢谢!

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 , ie witout an instance assigned. 你需要添加显式self因为telnet.Telnet.__init__不是绑定方法 ,而是一个未绑定的方法 ,即没有分配实例。 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)

You have to inherit from object , and you must put it after the old-style class you are trying to inherit from (so that object 's methods aren't found first): 您必须从object继承,并且必须将它放在您尝试继承的旧式类之后(以便首先找不到object的方法):

>>> class Instrument(telnetlib.Telnet,object):
...     def __init__(self, host=None, port=0, timeout=5):
...         super(Instrument,self).__init__(host, port, timeout)
...
>>> Instrument()
<__main__.Instrument object at 0x0000000001FECA90>

Inheriting from object gives you a new-style class which works with super . 继承自object为您提供了一个与super工作的新式类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM