【问题标题】:Function which take as input variable result from other function将其他函数的结果作为输入变量的函数
【发布时间】: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 值...

标签: python function


【解决方案1】:

需要返回值或引发异常:

if source in connection_source: return (connection_connectstr) else: raise configparser.NoSectionError('Something wrong')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2020-03-22
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    相关资源
    最近更新 更多