【发布时间】:2016-03-24 14:22:16
【问题描述】:
我无法理解我在这里做错了什么。我正在使用以下代码定义一个类:
import sqlite3 as lite
import sys
import os.path
class database:
def __init__(self,dbfname):
if os.path.isfile(dbfname):
self.con = lite.connect(dbfname)
else:
self.con = self.createDBfile(dbfname)
#Other methods...
然后,当我尝试创建类的实例时
base = database("mydb.db")
我收到一条错误消息,指出没有名为 dbfname 的“全局”变量。
Traceback (most recent call last):
File "testdb.py", line 67, in <module>
base = database("mydb.db")
File "testdb.py", line 13, in __init__
self.con = self.createDBfile(dbfname)
File "testdb.py", line 15, in createDBfile
if os.path.isfile(dbfname):
NameError: global name 'dbfname' is not defined
使用参数变量 dbfname 的正确方法是什么?
【问题讨论】:
-
这就是你的代码...?因为它已损坏和/或格式错误。请仔细检查您在此处粘贴的内容,尤其是缩进。
-
我修复了重复的“类数据库:”这行是你的意思吗?
标签: python class arguments init