问题描述
我有下面的代码读取配置文件,并将结果作为列表存储在一些变量中
import ConfigParser
$ b $ def read_config_file():
config = ConfigParser.ConfigParser()
cnf_path ='config_files / php.sr'
config.read(cnf_path)
如果config.has_section('basic'):
如果config.has_option('basic','basic'):
php_bsc_mdls = config.get('basic','basic')。split(' ,')
如果config.has_section('advance'):
如果config.has_option('advance','advance'):
php_adv_mdls = config.get('advance',' (',')
现在我想得到结果变量 php_bsc_mdls
和 php_adv_mdls
来自函数
,类似于 read_config_file.php_bsc_mdls
或 read_config_file.php_adv_mdls
所以有可能访问/从python函数中获取变量?
您只需要返回它们。功能结束后,它们不复存在。
def read_config_file():
config = ConfigParser.ConfigParser()
cnf_path ='config_files / php ('basic','basic'):
php_bsc_mdls ('advance','advance')= config.get('basic','basic')。split(',')
如果config.has_section('advance'):
如果config.has_option
php_adv_mdls = config.get('advance','advance')。split $ b elif php_bsc_mdls:
return php_bsc_mdls,None
其他方法将会是一个类,将它们保存到类变量中。然后从班上获取这些值,而不是函数。
或者像这样:
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)
如果config.has_section('basic'):
如果config.has_option('basic','basic'):
php_bsc_mdls = config.get('basic','basic')。split(' ,')
如果config.has_section('advance'):
如果config.has_option('advance','advance'):
php_adv_mdls = config.get('advance',' (',')
返回php_bsc_mdls,php_adv_mdls
无论哪种情况,您都需要检查返回值,以便您调用函数。如果这些值没有或没有。
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 functionsomething like read_config_file.php_bsc_mdls
or read_config_file.php_adv_mdls
So is it possible to access/get the variables from the python function ?
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.
这篇关于如何访问python函数中声明的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!