我正在尝试为我的模块创建一个全局字典,但它一直告诉我我的语法错误。我做错了吗?
_dummy_date = dict{
'0'=datetime(year=1990, month=1, day=1, hour=0,minutes=0,second=0),
'1'=datetime(year=1990, month=1, day=2, hour=0,minutes=0,second=0),
'2'=datetime(year=1990, month=1, day=3, hour=0,minutes=0,second=0),
'3'=datetime(year=1990, month=1, day=4, hour=0,minutes=0,second=0),
'4'=datetime(year=1990, month=1, day=5, hour=0,minutes=0,second=0),
'5'=datetime(year=1990, month=1, day=6, hour=0,minutes=0,second=0),
'6'=datetime(year=1990, month=1, day=7, hour=0,minutes=0,second=0)
}
这是错误
File "/usr/lib/python2.7/dist-packages/odoo/modules/module.py", line 367, in load_openerp_module
__import__('odoo.addons.' + module_name)
File "/usr/lib/python2.7/dist-packages/odoo/modules/module.py", line 81, in load_module
execfile(modfile, new_mod.__dict__)
File "/mnt/extra-addons/jy_attendance_base/__init__.py", line 3, in <module>
from . import models
File "/mnt/extra-addons/jy_attendance_base/models/__init__.py", line 3, in <module>
from . import models
File "/mnt/extra-addons/jy_attendance_base/models/models.py", line 8
_dummy_date = dict{
^
SyntaxError: invalid syntax
答案 0 :(得分:1)
dict()是一种方法,所以你必须明确地调用它:
dict()
此外,以这种方式构造的dict不是用等号(=)分隔,而是用冒号分隔,如下:
_dummy_date = dict({
'0':datetime(year=1990, month=1, day=1, hour=0,minutes=0,second=0),
'1':datetime(year=1990, month=1, day=2, hour=0,minutes=0,second=0),
'2':datetime(year=1990, month=1, day=3, hour=0,minutes=0,second=0),
'3':datetime(year=1990, month=1, day=4, hour=0,minutes=0,second=0),
'4':datetime(year=1990, month=1, day=5, hour=0,minutes=0,second=0),
'5':datetime(year=1990, month=1, day=6, hour=0,minutes=0,second=0),
'6':datetime(year=1990, month=1, day=7, hour=0,minutes=0,second=0)
})
但是,由于您明确地创建了词典,因此您可以删除dict()
来电,只需写下:
_dummy_date = {
'0':datetime(year=1990, month=1, day=1, hour=0,minutes=0,second=0),
'1':datetime(year=1990, month=1, day=2, hour=0,minutes=0,second=0),
'2':datetime(year=1990, month=1, day=3, hour=0,minutes=0,second=0),
'3':datetime(year=1990, month=1, day=4, hour=0,minutes=0,second=0),
'4':datetime(year=1990, month=1, day=5, hour=0,minutes=0,second=0),
'5':datetime(year=1990, month=1, day=6, hour=0,minutes=0,second=0),
'6':datetime(year=1990, month=1, day=7, hour=0,minutes=0,second=0)
}
这将简单地创建你喜欢的字典