下面是我的示例代码:
#!/usr/bin/env python3
LIST_OF_UNITS = {}
class Unit():
def __init__(self,name, value):
self.name = name
self.value = value
def create_new_unit(name, value):
return Unit(name, value)
def add_new_unit(d, name, value):
d[name] = Unit(name, value)
return d
unit = create_new_unit('reactor1', 1)
LIST_OF_UNITS[unit.name] = unit
unit = create_new_unit('reactor2', 2)
LIST_OF_UNITS[unit.name] = unit
LIST_OF_UNITS = add_new_unit(LIST_OF_UNITS, 'reactor3', 3)
print(LIST_OF_UNITS)
LIST_OF_UNITS = add_new_unit(LIST_OF_UNITS, 'reactor3', 4)
print(LIST_OF_UNITS)
如你所见,我有两种向字典添加对象的方法。还不知道哪个更好。解决我的问题可能更灵活些。所以,我把它们都包括进去了。你知道吗
我想建立一个反应堆及其属性的列表。你知道吗
对于每个reactor,我创建一个对象,该对象最终将包含该reactor的属性(比如它的体积、操作开始和结束时间等)
我想防止(重复)创建一个单元。在本例中,应避免创建值4的“reactor3”。你知道吗
最好的办法是什么。在类内部,使用其中一个方法还是其他方法?你知道吗
非常感谢您的真知灼见。你知道吗
如果向词典中添加具有相同键的条目,则该条目将被覆盖,因此将丢失其中一个条目。你知道吗
如果要避免这种情况,可以检查名称是否已经在字典的键值中。你知道吗
这个怎么样?你知道吗
如果您更改一些代码,您可以将所有创建的
Unit
作为类变量存储在Unit
中。工厂方法应该是classmethods,并将自动向其中添加/创建实例。你知道吗输出:
就我个人而言,我建议不要创建多个实例化新方法的方法。我可能会把“工厂方法”作为@classmethods,而不是放在普通程序中。这样
Unit
的所有内务处理都由Unit
类本身,您可以将逻辑集中在它所属的位置,而不必在主程序中添加创建的单元。你知道吗建议阅读
@classmethod
:Meaning of @classmethod and @staticmethod for beginner?只要检查一下这个项目是否已经在字典的键中。仅当尚未插入时插入。你知道吗
相关问题 更多 >
编程相关推荐