繁体   English   中英

当我在班级中使用班级时,如何调用原始班级的属性

[英]when i use a class within a class how can i call a upon attributes from the original class

调用其他类中的类时,如何调用嵌套路径中调用它的类中的属性

下面的示例是我自己尝试解决的尝试。 当然,这会导致错误,声称未定义“示例”,但我想我会展示一下以明确我正在尝试做什么

class CamelCase:
    def __init__(self):
        self.variable = 'foo'
        self.variable2  = CamelCase_2()


class CamelCase_2:
    def __init__(self):
        return example.variable        #this is the part i want to be able to do properly. 
                                       #using the attribute attribute 'variable' within CamelCase_2


example = CamelCase()
print(foo.variable2)

请注意,以上只是我正在尝试做的事情的简化。 假设我实际上试图在CameCase2中的example.variable上使用函数upper()

CamelCase需要将自身传递给CamelCase_2

class CamelCase:
    def __init__(self):
        self.variable = 'foo'
        self.variable2  = CamelCase_2(self)


class CamelCase_2:
    def __init__(self, parent):
        self.camelcase = parent

然后, CamelCase_2其他方法可以引用self.camelcase来访问创建它的对象。

暂无
暂无

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

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