Python:创建一个Class,哪个属性是来自另一个类的对象?

时间:2016-03-23 16:06:38

标签: python class object nested

是否可以创建一个具有某些属性=其他类的对象的类?

类传感器:

def __init__(self):
    # The attributes of the class that will be available for external use
    self.temperature = None
    self.humidity = None
    self.distance = None
    self.light = None
    self.sound = None
    self.url = None
    self.base_url = "http://..."

    # Instantiating sensor objects
    self.SOUND = NEW OBJECT FROM CLASS SOUND
    self.LIGHT = NEW OBJECT FROM CLASS LIGHT
    self.DISTANCE = NEW OBJECT FROM CLASS DISTANCE
    self.TEMP = NEW OBJECT FROM CLASS TEMPERATURE

1 个答案:

答案 0 :(得分:1)

试过吗?

>>> class A(object):
...   pass
...
>>> class B(object):
...   a = A()
...
>>> b = B()
>>> b.a
<__main__.A object at 0x021490B0>