Python将字典分配给属性

Python将字典分配给属性,python,class,Python,Class,我有一个a班,像这样: class equipment(): def __init__(self, l, d): self.length = {"desc": "Length of equipment", "val":l, "unit":"ft"} self.diameter = {"desc": "Diameter of equipment", "val":d, "unit":"inch"} 所以我需要一个属性行的构造函数 class fis

我有一个a班,像这样:

class equipment():
     def __init__(self, l, d):
          self.length = {"desc": "Length of equipment", "val":l, "unit":"ft"}
          self.diameter = {"desc": "Diameter of equipment", "val":d, "unit":"inch"}
所以我需要一个属性行的构造函数

class fisicalProp():
   def __init_(self, d, v, u)
       self.something = {"desc": "Diameter of equipment", "val":d, "unit":"inch"}
那么我只会打电话

class equipment():
         def __init__(self, l, d):
              self.length = fisicalPropertie("Length of equipment",l, "ft"}
              self.diameter = fisicalPropertie("Diameter of equipment", d, "inch"}

你能帮我改进这样的代码吗?

为什么要把这个操作复杂化?如果将此类用于多个实例,则不需要在此处创建dict。因此,如果不打算使用继承,则不需要将()与类定义一起使用

您不需要类,只需要返回字典的函数:
def fisicalproperty(desc,val,unit):
class equipment:
    def __init__(self, length, len_units, diameter, dia_units):
        self.length = (length, len_units)
        self.diameter = (diameter, dia_units)

eq = equipment(1, "ft", 2, "inch")
print(eq.length, eq.diameter)