gpt4 book ai didi

python - 定义实例变量时的最佳实践

转载 作者:太空狗 更新时间:2023-10-29 16:55:56 25 4
gpt4 key购买 nike

我是 Python 的新手,对以下类(class)有疑问:

class Configuration:
def __init__(self):
parser = SafeConfigParser()
try:
if parser.read(CONFIG_FILE) is None:
raise IOError('Cannot open configuration file')
except IOError, error:
sys.exit(error)
else:
self.__parser = parser
self.fileName = CONFIG_FILE

def get_section(self):
p = self.__parser
result = []
for s in p.sections():
result.append('{0}'.format(s))
return result

def get_info(self, config_section):
p = self.__parser
self.section = config_section
self.url = p.get(config_section, 'url')
self.imgexpr = p.get(config_section, 'imgexpr')
self.imgattr1 = p.get(config_section, 'imgattr1')
self.imgattr2 = p.get(config_section, 'imgattr2')
self.destination = p.get(config_section, 'destination')
self.createzip = p.get(config_section, 'createzip')
self.pagesnumber = p.get(config_section, 'pagesnumber')

是否可以在另一个函数中添加更多实例变量,在本例中为 get_info,还是最好在构造函数中定义所有实例变量?如果我到处定义新的实例变量,它不会导致意大利面条代码吗?

编辑:我将这段代码与一个简单的图像抓取工具一起使用。通过 get_section 我返回配置文件中的所有部分,然后遍历它们以访问我正在从中抓取图像的每个站点。对于每次迭代,我都会调用 get_section 以获取配置文件中每个部分的配置设置。如果有人能想出另一种方法就好了!谢谢!

最佳答案

我肯定会在 __init__ 中声明所有实例变量。不这样做会导致复杂性增加和潜在的意外副作用。

为了提供 David Hall 在访问方面的另一种观点,本文来自 Google Python style guide .

Access Control:

If an accessor function would be trivial you should use public variables instead of accessor functions to avoid the extra cost of function calls in Python. When more functionality is added you can use property to keep the syntax consistent

On the other hand, if access is more complex, or the cost of accessing the variable is significant, you should use function calls (following the Naming guidelines) such as get_foo() and set_foo(). If the past behavior allowed access through a property, do not bind the new accessor functions to the property. Any code still attempting to access the variable by the old method should break visibly so they are made aware of the change in complexity.

From PEP8

For simple public data attributes, it is best to expose just the attribute name, without complicated accessor/mutator methods. Keep in mind that Python provides an easy path to future enhancement, should you find that a simple data attribute needs to grow functional behavior. In that case, use properties to hide functional implementation behind simple data attribute access syntax.

Note 1: Properties only work on new-style classes.

Note 2: Try to keep the functional behavior side-effect free, although side-effects such as caching are generally fine.

Note 3: Avoid using properties for computationally expensive operations; the attribute notation makes the caller believe that access is (relatively) cheap.

Python 不是 java/C#,它对代码的外观和编写方式有非常强烈的想法。如果您使用 Python 进行编码,那么让它看起来和感觉起来都像 Python 是有意义的。其他人将能够更轻松地理解您的代码,您也将能够更好地理解其他 Python 代码。

关于python - 定义实例变量时的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10396920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com