Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:创建一个类,哪些属性是来自其他类的对象?_Python_Class_Object_Nested - Fatal编程技术网

Python:创建一个类,哪些属性是来自其他类的对象?

Python:创建一个类,哪些属性是来自其他类的对象?,python,class,object,nested,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

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

类别传感器:

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
你试过了吗

>>A类(对象):
...   通过
...
>>>B类(对象):
...   a=a()
...
>>>b=b()
>>>文学学士

为什么它不能工作
None
和字符串也是其他对象。好的,谢谢,所以我的问题不在类初始化中,而是在类delcaration的另一部分中。
>>> class A(object):
...   pass
...
>>> class B(object):
...   a = A()
...
>>> b = B()
>>> b.a
<__main__.A object at 0x021490B0>