简体   繁体   English

创建不直接访问父类__init __()函数的子类

[英]Creating a subclass without direct access to the parent class __init__() function

I'm using the DroneKit API in Python for controlling a drone using a companion computer. 我正在Python中使用DroneKit API来使用配套计算机来控制无人机。 I'm trying to create a class, Vehicle, which inherits from the Vehicle class in DroneKit. 我正在尝试创建一个类Vehicle,该类继承自DroneKit中的Vehicle类。 The purpose of this class is for me to override some methods present in DroneKit that don't work with PX4 as well as adding a few methods of my own, whilst still having access to all of the methods available by default. 此类的目的是让我重写DroneKit中存在的某些不适用于PX4的方法,并添加一些我自己的方法,同时仍可访问默认情况下的所有可用方法。

The issue is that you don't create a Vehicle object directly using Dronekit – you call the connect() function which return a Vehicle object. 问题是您没有直接使用Dronekit创建Vehicle对象,而是调用connect()函数来返回Vehicle对象。

My question is, how do I create an instance of my class? 我的问题是,如何创建类的实例?

The accepted method seems to be to call the parent init (), like so: 可接受的方法似乎是调用父init (),如下所示:

class Vehicle(dronekit_Vehicle):
    def __init__(self, stuff):
        dronekit_Vehicle.__init__(stuff)

But like I said, you don't create a Vehicle object directly in Dronekit, eg vehicle = Vehicle(stuff), but by vehicle = connect(stuff), which eventually returns a Vehicle object but also does a bunch of other stuff. 但是就像我说的那样,您不是直接在Dronekit中创建Vehicle对象,例如vehicle = Vehicle(stuff),而是通过vehicle = connect(stuff),这最终会返回Vehicle对象,但还会执行其他操作。

The only way I can think of is 我能想到的唯一方法是

class Vehicle(dronekit_Vehicle):
    def __init__(self, stuff):
        self.vehicle = connect(stuff)

And then having to use self.vehicle.function() to access the default DroneKit commands and attributes, which is a huge pain. 然后必须使用self.vehicle.function()来访问默认的DroneKit命令和属性,这是一个很大的麻烦。

How do I make this work? 我该如何工作?

The way objects are defined has nothing to do with connect . 定义对象的方式与connect无关。 Calling connect is merely some convenience function that wraps some logic around the object creation : 调用connect 只是一些便利功能,它围绕对象创建包装了一些逻辑

def connect(...):
    handler = MAVConnection(...)
    return Vehicle(handler)

with Vehicle.__init__() being defined as Vehicle.__init__()定义为

def __init__(self, handler):
    super(Vehicle, self).__init__()
    self._handler = handler
    ...

So as long as you pass on the handler in your constructor: 因此,只要您在构造函数中传递处理程序:

class MyVehicle(dronekit.Vehicle):
    def __init__(self, handler):
      super(MyVehicle, self).__init__(handler)

Your class will work with connect() : 您的班级将使用connect()

connect(..., vehicle_class=MyVehicle)

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

相关问题 在不定义__init__函数的情况下创建类的实例 - Creating an instance of a class without defining the __init__ function 父类中的__getattr__导致子类__init__递归错误 - __getattr__ in parent class causing subclass __init__ recursion error 为什么在父类__init __()中调用super()会改变子类__init __()的行为? - Why does calling super() in parent class __init__() change the subclass __init__() behavior? 调用类函数而不触发 __init__ - Calling a class function without triggering the __init__ 了解在子类中调用父级__init__ - Understanding calling parent __init__ in subclass 使用和不使用 __init__() 的正确参数创建类的实例有什么区别? - What is the difference creating an instance of a class with and without the correct parameter of __init__()? 我可以不使用__init__来访问课程吗? -Python - Can I access a class without an __init__? - Python 在 OrderedDict 子类中覆盖没有 super() 的 __init__ 但父构造函数仍然有效 - Overwriting __init__ without super() in OrderedDict subclass but parent constructor still works 扩展 class 而不访问其 __init__ 方法 - Extend a class without access to its __init__ method __init__() 应该调用父类的 __init__() 吗? - Should __init__() call the parent class's __init__()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM