如何从另一个对象的方法(这是Python中的属性之一)访问对象的属性?

如何从另一个对象的方法(这是Python中的属性之一)访问对象的属性?

问题描述:

我想知道是否可能,如果可以,在实现组合时如何访问超级"类实例的属性.

I would like to know if it's possible, and if yes, how to access attribute(s) of a "super" class instance, when having composition implemented.

下面提供的示例仅是为了在此处提供想法,并为进一步的说明建立共识.

Example provided below is only to provide idea here and setup common ground on further explanations.

我想直接从对象门"(类型DoorElement)访问MiniVan实例的"id"属性.

I want to have access to "id" attribute for an instance of MiniVan directly from object "door" (type DoorElement).

我的代码

class Car:
    def __init__(self, _id):
        self.id = _id


class CarElement:
    def __init__(self, name):
        self.name = name

    def get_car_id(self):
        # Body which will access value of attribute "id"
        return car_id


class MiniVan(Car):
    def __init__(self, _id):
        super(MiniVan, self).__init__(_id)
        self.door = DoorElement('door')
        self.engine = EngineElement('engine')


class DoorElement(CarElement):
    def __init__(self, name):
        super(DoorElement, self).__init__(name)


class EngineElement(CarElement):
    def __init__(self, name):
        super(EngineElement, self).__init__(name)


def main():
    mini_van = MiniVan(123)
    id_from_door = mini_van.door.get_car_id()
    id_from_engine = mini_van.engine.get_car_id()
    print(id_from_door) # Expected output 123
    print(id_from_engine) # Expected output 123


if __name__ == '__main__':
    main()


预期:

  • 打印两次"123"

我尝试过的事情:

  1. 在创建对象时传递必需的属性

我知道我可以通过传递"car_id"来定义 init 方法,但是出于某些原因,我希望尽可能避免使用它.如果没有的话,我可能会去做.

I know that I could just define init method with passing "car_id" but for some reasons I would love to avoid it if possible. If not, I would propably just go for it.

  1. 设置类属性,然后在类方法中从CarElement类调用它,例如:
  1. to set class attribute, and then call it from CarElement class within classmethod e.g.:

@classmethod
def get_id(cls):
    return Car.id

但是这种解决方案的问题在于,我可以为Car类设置许多子类(MiniVan,Truck等),并且希望它仍然可以正常工作.

But issue with this solution is that, I can have many child-classes for Car class (MiniVan, Truck, etc.) and I want have it still working.

  1. 尝试使用描述符

def __get__(self, instance, owner):
    return instance.id

但是我可以理解为错误,实际上getter(据我了解干净的代码)应该返回类的实例,而不是任何属性.

But I could understand it wrong, and actually getter (as far as I understand clean code) should return instance of a class and not any attribute.

其他信息

  • 我将始终使用CarElement(或子类)实例作为Car(或子类)实例的实例的属性-不同的用法将被视为使用错误
  • Car类可以有很多不同的子类,但总是在继承方式内(Car<-RacingCar(Car)<-FormulaOneCar(RacingCar))但没有组成

为使代码正常工作,您必须使用 car_id 初始化所有 CarElement -s.当前,您遇到的错误是由于方法范围内缺少此类变量而引起的.我的更改想法是这样的:

In order for your code to work, you would have to initialize all CarElement-s with car_id. Currently, the error you are getting comes from lack of such a variable in the scope of the method. My idea of a change is this:

class CarElement:
    def __init__(self, name, car_id):
        self.name = name
        self.car_id = car_id

    def get_car_id(self):
        # Body which will access value of attribute id
        return self.car_id

我看不到其他任何魔术方式.

I can't see any other magic way.