假设我有代码:
class NoneDict(dict):
def __getitem__(self, name):
try:
return super(NoneDict, self).__getitem__(name)
except:
return None
我希望人们能够创建一个NoneDict对象,而无需编写所有这些代码。我尝试将其包含在模块中,然后输入:
import nonedict
foo = NoneDict()
但它没有用。我怎样才能让有人可以导入模块然后能够创建一个非编辑器而无需输入所有代码?
答案 0 :(得分:3)
import nonedict
foo = nonedict.NoneDict()
或
from nonedict import NoneDict
foo = NoneDict()
或(感谢@Joel Cornett)
from nonedict import NoneDict as nd
foo = nd()
答案 1 :(得分:2)