简体   繁体   English

如何访问在python中的函数内部声明的变量

[英]How to access the variables declared inside functions in python

I have the following code that reads the configuration file and stores the results in some variables as a list 我有以下代码读取配置文件并将结果存储在一些变量中作为列表

import ConfigParser

def read_config_file():
    config = ConfigParser.ConfigParser()
    cnf_path = 'config_files/php.sr'
    config.read(cnf_path)
    if config.has_section('basic'):
        if config.has_option('basic', 'basic'):
            php_bsc_mdls = config.get('basic', 'basic').split(',')
    if config.has_section('advance'):
        if config.has_option('advance','advance'):
            php_adv_mdls = config.get('advance', 'advance').split(',')

Now i want to get the result variables php_bsc_mdls and php_adv_mdls from the function something like read_config_file.php_bsc_mdls or read_config_file.php_adv_mdls 现在我想要得到的结果变量php_bsc_mdlsphp_adv_mdls从功能类似read_config_file.php_bsc_mdlsread_config_file.php_adv_mdls

So is it possible to access/get the variables from the python function ? 那么可以从python函数访问/获取变量吗?

You just need to return them. 您只需要退货即可。 They cease to exist when the function ends. 当函数结束时,它们不再存在。

def read_config_file():
    config = ConfigParser.ConfigParser()
    cnf_path = 'config_files/php.sr'
    config.read(cnf_path)
    if config.has_section('basic'):
        if config.has_option('basic', 'basic'):
            php_bsc_mdls = config.get('basic', 'basic').split(',')
    if config.has_section('advance'):
        if config.has_option('advance','advance'):
            php_adv_mdls = config.get('advance', 'advance').split(',')

    if php_bsc_mdls and php_adv_bls:
        return php_bsc_mdls,php_adv_mdls
    elif php_bsc_mdls:
        return php_bsc_mdls, None

Other approach would be a class where you save them to class variables. 另一种方法是将类保存到类变量中的类。 And later get those values from the class, not the function. 然后从类而不是函数中获取这些值。

Or like this: 或像这样:

def read_config_file():
    php_bsc_mdls = None
    php_adv_mdls = None
    config = ConfigParser.ConfigParser()
    cnf_path = 'config_files/php.sr'
    config.read(cnf_path)
    if config.has_section('basic'):
        if config.has_option('basic', 'basic'):
            php_bsc_mdls = config.get('basic', 'basic').split(',')
    if config.has_section('advance'):
        if config.has_option('advance','advance'):
            php_adv_mdls = config.get('advance', 'advance').split(',')

    return php_bsc_mdls, php_adv_mdls

In either case, you need to check the return values where ever you call the function. 无论哪种情况,都需要在调用函数的任何地方检查返回值。 If the values are none or not. 值是否为none。

def read_config_file():
  php_bsc_mdls, php_adv_mdls = None, None # Init to None.
  ...
  return php_bsc_mdls, php_adv_mdls # Return at end.

# Get results
basic, advanced = read_config_file()

Alternatively, you could just make a class for this. 或者,您可以为此做一个课程。

class Config:
  php_adv_mdls = None
  php_bsc_mdls = None
  @staticmethod
  def read_config_file():
    if not Config.php_adv_mdls and not Config.php_bsc_mdls:
      # Load the data here.
      config = ConfigParser.ConfigParser()
      ...
      Config.php_adv_mdls = config.get...
      Config.php_bsc_mdls = config.get...

Use the class variables and static methods to load your config files once . 使用类变量和静态方法一次加载您的配置文件。

Config.php_adv_mdls # None
Config.read_config_file() # Loads the data from config.
Config.read_config_file() # Will only attempt another load if either 
                          # class variable in not valid.
Config.php_bsc_mdls       # If successful, will be initialize with data.

As it was said it would be reasonable to do this with return. 如前所述,这样做是合理的。

But... 但...

In python functions are objects, so you can do smth like this: 在python函数中是对象,因此您可以执行以下操作:

def a():
    a.x = 10

a()
print a.x    # >> 10

Last though not an example of good code, in this case. 在这种情况下,最后虽然不是好的代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在其他函数中声明的变量在主 python 3 中不起作用? - Variables declared inside other functions are not working in main python 3? 如何在python中声明变量? - How variables are declared in python? 如何在XSLT中使用python函数和变量? - How to use python functions and variables inside XSLT? 如何从python中的其他函数访问变量 - How to access variables from other functions in python 如何访问闭包内的外部函数变量(python 2.6)? - How do I access outer functions variables inside a closure(python 2.6)? 如何访问在 Python 中的 IF 语句之外的 IF 语句中声明的变量 - How to access a variable declared inside IF statement outside IF statement in Python Python:使用在if语句中声明的变量 - Python: using variables declared inside if statement 我如何访问在我的 class 的 __init__ 中声明的变量之一 - How can I access variables declared in __init__ of my class in one of it's functions 在Pycharm中,当声明为全局赋值时,全局变量不会在函数内部自动完成 - In Pycharm, global variables are not autocompleted inside functions when declared global for assignment 如何在SWIG中访问声明的模板结构变量? - How to access declared template structure variables in SWIG?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM