【发布时间】:2018-03-02 11:20:10
【问题描述】:
共有三个类:
A、B 和 C
B 的 __init__ 创建了 A 的对象。使用 mutators,我将能够为创建的实例从 B 更改 A 的属性。
但是,如果不将 Object 显式传递给 __init__ 方法 [not C.__init(self, object: A) ]
有什么方法可以隐式允许 C 使用 A 的实例?
我是 python 新手,不确定这是否是一个有效的问题。我查看了将对象显式传递给 C 类的其他来源
class A:
def __init__(self):
x = []
y = []
class C :
def __init__(self):
#[get obj1 without passing the instance in init]
self.value = None
def method1():
self.value = len([]) #len(obj1 of A.x)
class B:
def __init__(self):
obj1 = A()
obj1.x = [1,2,3,4]
obj1.y = [1,2,3]
obj2 = B()
print(obj2.value) #this should be the length of x in the instance A created above
【问题讨论】:
-
任何一位禅宗斗士都应该发帖,明确的胜于隐含的。
标签: python python-2.7 python-3.x class oop