我是python的新手,我的背景是VB。我收到错误“NameError:name'GetASetting'未定义”。
日志:
[INFO ] Kivy v1.8.0
Purge log fired. Analysing...
Purge 60 log files
Purge finished !
[INFO ] [Logger ] Record log in C:\Users\Sudheer\.kivy\logs\kivy_14-07-18_10.txt
[INFO ] [Factory ] 157 symbols loaded
[DEBUG ] [Cache ] register <kv.lang> with limit=None, timeout=Nones
[DEBUG ] [Cache ] register <kv.image> with limit=None, timeout=60s
[DEBUG ] [Cache ] register <kv.atlas> with limit=None, timeout=Nones
[INFO ] [Image ] Providers: img_tex, img_dds, img_pygame, img_gif (img_pil ignored)
[DEBUG ] [Cache ] register <kv.texture> with limit=1000, timeout=60s
[DEBUG ] [Cache ] register <kv.shader> with limit=1000, timeout=3600s
Traceback (most recent call last):
File "C:\Kivy180\Python33\lib\runpy.py", line 160, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Kivy180\Python33\lib\runpy.py", line 73, in _run_code
exec(code, run_globals)
File "D:\OS Files\workspace\Org\__main__.py", line 7, in <module>
from formcontrol import FormControl
File "D:\OS Files\workspace\Org\formcontrol.py", line 8, in <module>
from login.logincodes import LoginControl
File "D:\OS Files\workspace\Org\login\logincodes.py", line 7, in <module>
from dbcodes.logins import LoginAccess
File "D:\OS Files\workspace\Org\dbcodes\logins.py", line 2, in <module>
from dbcodes.settings import GetASetting, SettingList
File "D:\OS Files\workspace\Org\dbcodes\settings.py", line 31, in <module>
class SettingList(object):
File "D:\OS Files\workspace\Org\dbcodes\settings.py", line 36, in SettingList
FirstRun_Get = GetASetting(FirstRun)
NameError: name 'GetASetting' is not defined
class和def都在同一个.py文件中。 代码:
def Initiation():
from os import path
print(Getcwd())
folderpath=str(Getcwd())
fpath = folderpath + "/orgapp.ini"
dbpath = folderpath + "/orgapp.db"
if path.exists(fpath)==False:
#Writing Basic Values
f = open(fpath,'w')
setlist=SettingList()
f.write(setlist.FirstRun+'|True' + '\n')
f.write(setlist.IniPath+'|'+fpath + '\n')
f.write(setlist.DBPath+'|'+dbpath + '\n')
f.close()
print('File Created')
#Creating default database
CreateDB(dbpath)
return True
else:
print('File exists')
return False
def GetASetting(settingtype):
if settingtype=='': return None
path = Getcwd() + '/orgapp.ini'
f1=open(path,'r')
for k in f1:
k=k.replace('\n','')
c= (k.rsplit(sep='|', maxsplit=2))
if settingtype.lower() == c[0].lower():
f1.close()
if c[1]=='': return None
else: return c[1]
f1.close()
return None
class SettingList(object):
FirstRun = 'FirstRun'
IniPath='IniPath'
DBPath='DBPath'
FirstRun_Get = GetASetting(FirstRun)
IniPath_Get = GetASetting(IniPath)
DBPath_Get = GetASetting(DBPath)
def Getcwd():
from os import getcwd
p=''
p=getcwd()
p=p.replace('\\', '/')
return p
def CreateDB(dbpath):
import sqlite3
conn = sqlite3.Connection(dbpath, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
conn.execute('''
create table login
(loginid text, password text)
''')
#default user
id='admin'
pw='1234'
conn.execute("insert into login (loginid, password) values (?,?)",(id,pw))
conn.commit()
conn.close()
我通过在课堂上放置def做了一些工作,但是,上面的代码应该可以工作,请你告诉我上面做了什么错误,我检查了名字并且它们是正确的?
答案 0 :(得分:2)
GetASetting
需要存在才能使用它。使用代码的结构方式,它不会。在类定义之前定义GetASetting
。
答案 1 :(得分:1)
正如Ignacio所说,GetASetting
需要存在才能使用它。确切的原因是:类定义以及函数签名(其中参数的默认值可能包含可执行代码)在Python解释器第一次遇到它们时执行 - 因此,您的函数GetASetting
需要存在此时已经。 (另一方面,这也意味着您也可以在类定义中使用if/else
和其他控制流语句。)
无论如何,如果你不想要这个(并且你通常不会因为你遇到的那些不直观的错误),你应该为你的班级使用一个构造函数:
class SettingList(object):
# All code in here is executed when the interpreter first
# encounters the class definition
def __init__(self): # The same applies for this line
# This code only run when __init__() is actually called.
self.FirstRun = 'FirstRun'
self.IniPath='IniPath'
self.DBPath='DBPath'
# Now, the function definition below poses no problem.
self.FirstRun_Get = GetASetting(FirstRun)
self.IniPath_Get = GetASetting(IniPath)
self.DBPath_Get = GetASetting(DBPath)
def GetASetting(settingtype):
# […]
return None
# usage example
settings = SettingList()
print(settings.FirstRun_Get)
就可测试性而言,这也是一个好主意 - 现在每个SettingList实例都是在创建时初始化的,理论上,你可以模拟文件访问之类的依赖关系,即磁盘上的设置文件。