[英]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_mdls
和php_adv_mdls
从功能类似read_config_file.php_bsc_mdls
或read_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.