【发布时间】:2016-08-04 09:01:31
【问题描述】:
由于我是 python 编码的新手,我坚持定义一个函数,该函数将其他函数的输入变量作为输入变量。通常我的代码是:
#!/usr/bin/python
import configparser
import re
var="abc"
class Config:
def getSources(var):
config = configparser.ConfigParser()
config.read("C\\configFile.ini")
connection=config.get(connectinfo,'connectStr')
if source in connection_source:
print (connection_connectstr)
else:
print ('Something wrong')
return connection
try:
emp1 = Config.getSources(var)
except configparser.NoSectionError:
print ("Senction is not correct")
def getTables(connection)
简而言之,我从 Config.ini 文件中获取数据,然后进行一些处理。接下来我想创建下一个函数 getTables ,它作为输入从第一个函数中获取结果。顺便说一句,在 getSources 中,我想将其作为 return 语句,不幸的是 return 返回 null ...打印工作正常。
【问题讨论】:
-
完全不清楚...您只想在您的
getSources函数中添加return connection_connectstr吗? -
您希望 getSources 返回什么?该函数中没有 return 语句,因此您应该添加一个,例如
return connection。要让 getTables 使用该结果,请编写connection = getSources(var)、getTables(connection)。顺便说一句,connection_connectstr在 getSources 中是未定义的。 -
究竟是什么不清楚?看看 getSource 函数中的连接变量。然后我尝试在 getTables 函数中使用它。
-
所以
return connection可能在getSource中的某个位置(如果可能,在最后)connection的范围是本地的,在getSource之外不存在连接 -
大家好,我添加了返回连接。但问题是它返回 NULL 值...