简体   繁体   English

如何在Python中从基类访问派生类的实例变量

[英]How to access an instance variable of a derived class from a base class in Python

I have the next code in Python: 我有Python中的下一个代码:

class Base_class():
    def __init__(self):
        pass

    # this is the method where I need help
    def compress(variable?,index?):
        somecode()

class Derived_class_1():   
    def __init__(self,somelist):
        self.A = []
        self.B = []
        for item in somelist:
            if item == 1:
                self.A.append([1,0])
                self.B.append([0,1])
            else:
                self.A.append([1,0])
                self.B.append([1,0])

class Derived_class_2(): 
    def __init__(self,somelist):
        self.A = []
        self.B = []
        for item in somelist:
            if item == 1:
                self.A.append([1,0])
                self.B.append([1,0])
            else:
                self.A.append([1,0])
                self.B.append([0,1])

I need to be able in the compress method from the base class to access each of the instance values and return a list of one dimension based on the index given that the instance variable has two possible values for each element in the list. 我需要在基类的compress方法中能够访问每个实例值,并基于索引返回一维列表,因为实例变量对于列表中的每个元素都有两个可能的值。 Do I need to make the compress method a class one? 我需要使compress方法成为一类吗? And if it is the case, how can I make it possible? 如果是这种情况,我该如何实现呢?

2 problems with your code I see. 我的代码有2个问题。

  • Derrived classes don't inherit from Base_class Derrived类不会从Base_class继承
  • self is missing from compress compress缺少self

Changes: 变化:

class Base_class():
    # ...
    def compress(self, variable, index):
        print(self.A)
        print(self.B)

class Derived_class_1(Base_class):
    # ...

class Derived_class_2(Base_class):
    # ...

Then to test 然后测试

>>> x = Derived_class_1([1, 2])
>>> x.compress(5, 6)
[[1, 0], [1, 0]]
[[0, 1], [1, 0]]

>>> y = Derived_class_2([1, 2])
>>> y.compress(5, 6)
[[1, 0], [1, 0]]
[[1, 0], [0, 1]]

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

相关问题 Python - 如何从派生的 class 更新基础 class 变量 - Python - How to update base class variable from derived class Python 类型注释,例如派生自抽象基础 class 的 class 实例 - Python type annotations for instance of class derived from abstract base class 如何在Python3的基类中访问派生类的类属性? - How to access class attributes of a derived class in the base class in Python3? 来自派生类实例的 Pickle 基类 - Pickle base class from derived class instance Python:如何通过派生类实例访问父类对象? - Python: How to access parent class object through derived class instance? 派生类是基类的实例吗? - Is the derived class an instance of the base class? 访问派生类中更改的基类实例属性 - Access changed base class instance attribute in derived class 如何从Python的派生类中获取父类实例的名称? - How to get the name of the instance of the parent class from the derived class in Python? 如何在Python中从一个类访问类方法和类变量到另一个类的实例方法? - How to access class method and class variable from a class to another class's instance method in Python? 无法从 python 中的 class 实例访问变量 - Can not access variable from an instance of a class in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM