Python 如何引用_init__超类中定义的函数
我的Python 如何引用_init__超类中定义的函数,python,inheritance,init,Python,Inheritance,Init,我的\uuuu init\uuuu类中有一个帮助函数(def convert(dictionary),用于帮助配置设置。定义如下: class Configuration: def __init__(self, config_file=None, config=None): if config_file is not None: with open(config_file) as in_file: self._c
\uuuu init\uuuu
类中有一个帮助函数(def convert(dictionary
),用于帮助配置设置。定义如下:
class Configuration:
def __init__(self, config_file=None, config=None):
if config_file is not None:
with open(config_file) as in_file:
self._config = yaml.load(in_file, Loader=yaml.FullLoader)
elif config is not None:
self._config = config
else:
raise ValueError("Could not create configuration. Must pass either location of config file or valid "
"config.")
def convert(dictionary):
return namedtuple('Config', dictionary.keys())(**dictionary)
self.input = convert(self._config["input"])
self.output = convert(self._config["output"])
self.build = convert(self._config["build_catalog"])
class BuildConfiguration(Configuration):
def __init__(self, config_file=None, config=None):
super().__init__(config_file, config)
self.input = convert(self._config["input"])
self.output = convert(self._config["output"])
self.build = convert(self._config["build_catalog"])
这允许我在\uuuuu init\uuuu
内进行调用,如下所示:
class Configuration:
def __init__(self, config_file=None, config=None):
if config_file is not None:
with open(config_file) as in_file:
self._config = yaml.load(in_file, Loader=yaml.FullLoader)
elif config is not None:
self._config = config
else:
raise ValueError("Could not create configuration. Must pass either location of config file or valid "
"config.")
def convert(dictionary):
return namedtuple('Config', dictionary.keys())(**dictionary)
self.input = convert(self._config["input"])
self.output = convert(self._config["output"])
self.build = convert(self._config["build_catalog"])
class BuildConfiguration(Configuration):
def __init__(self, config_file=None, config=None):
super().__init__(config_file, config)
self.input = convert(self._config["input"])
self.output = convert(self._config["output"])
self.build = convert(self._config["build_catalog"])
因为我要设置多个配置,所以我希望继承他的类,如下所示:
class Configuration:
def __init__(self, config_file=None, config=None):
if config_file is not None:
with open(config_file) as in_file:
self._config = yaml.load(in_file, Loader=yaml.FullLoader)
elif config is not None:
self._config = config
else:
raise ValueError("Could not create configuration. Must pass either location of config file or valid "
"config.")
def convert(dictionary):
return namedtuple('Config', dictionary.keys())(**dictionary)
self.input = convert(self._config["input"])
self.output = convert(self._config["output"])
self.build = convert(self._config["build_catalog"])
class BuildConfiguration(Configuration):
def __init__(self, config_file=None, config=None):
super().__init__(config_file, config)
self.input = convert(self._config["input"])
self.output = convert(self._config["output"])
self.build = convert(self._config["build_catalog"])
但是,我无法从父类访问convert
。我还尝试了以下方法:
self.input=super()
这似乎也不起作用
因此,问题是如何从子类访问super()。\uuuu init\uuuuu
中定义的函数?您不能。每次调用\uuuu init\uuuuu
时都会创建一个新函数,该函数被丢弃,它不存在于函数之外。注意,这也适用于namedtuple创建的类('Config',dictionary.keys())(**dictionary)
。继续创建所有这些不必要的类是不好的,这完全违背了namedtuple
用于创建内存效率高的记录类型的目的。在这里,每个实例都有自己的类
以下是您应该如何定义它:
Config = namedtuple('Config', "foo bar baz")
def convert(dictionary): # is this really necessary?
return Config(**dictionary)
class Configuration:
def __init__(self, config_file=None, config=None):
if config_file is not None:
with open(config_file) as in_file:
self._config = yaml.load(in_file, Loader=yaml.FullLoader)
elif config is not None:
self._config = config
else:
raise ValueError("Could not create configuration. Must pass either location of config file or valid "
"config.")
self.input = convert(self._config["input"])
self.output = convert(self._config["output"])
self.build = convert(self._config["build_catalog"])
虽然在这一点上,它似乎更清洁,只是使用
Config(**self._config["input"])
如果你想访问它,就在\uuuu init\uuuuuuu
之外定义它们,方法是在模块名称空间中创建一个方法,或者巧妙地在模块名称空间中创建一个方法,这样嵌套函数从来都不是一件好事(除非你真的必须这样做).由于转换
函数与类配置
没有任何关系,只需将其设置为模块
中的正常函数
,从逻辑上讲,定义转换
仅在调用\uuuuuuuuu
时创建,它仅存在于其范围内,并且在\uuuuuuuuu init>时被丢弃__代码>结束。因此,不,它不存在于它之外。此外,在每次调用函数时,您都会创建另一个namedtuple
类。整个设置都很糟糕。在外部定义namedtuple
类,这样您就只创建其中一个,并在全局范围内或作为一个方法定义该助手函数。@juanpa.arrivillaga Sure,如果您要返回它,它很好。嘿,这是我们为装饰者所做的。但是在这种情况下,它没有任何意义,是的,对最后一种方法很满意。谢谢。