python:如何组织类中的方法
我遇到过这样的情况,我实例化了一个类,该类需要很长的计算来计算多个属性。 下面是一个例子。
class Father():
def __init__(self, fathername, age):
self.fathername = fathername
self.age = age
self.initials = self.initials()
self.initials2 = self.initials2()
self.salary = self.salary(self.age)
# using internal methods
def initials(self):
'''
long calculation
'''
return self.fathername[0:1]
def initials2(self):
'''
long calculation based on initials1 and on many previously calculated attributes
'''
return self.initials.upper()
@staticmethod
def salary(age):
'''
long calculation
'''
result = age*1000
return result
问题是接下来的路是什么。 a) 根本没有方法,所有代码放在一起,计算属性时只需设置它们
# peseudocode:
self.attribute = XX
# further calculations
self.attribute2 = YY
b) 如上面的示例代码所示。使用内部方法来分离功能。包括返回传递给相应属性的值的类方法。优点是可以很好地安排 init 中的所有属性,
从而使用静态方法提供了一个很好的概述 c)。我认为这不是正确的方法,因为永远不会
使用 @property 从类 d) 之外调用它们。我认为这不是正确的方法,因为这意味着每次调用属性时都会重新运行方法。
我会选择选项b,但不知怎的,我感觉不太Pythonic。
假设方法中的代码很长,还有哪些其他方法可以对该类进行编码?
谢谢
I have the situation in which I instantiate a class that has long calculations to calculate several attributes.
The following is an example.
class Father():
def __init__(self, fathername, age):
self.fathername = fathername
self.age = age
self.initials = self.initials()
self.initials2 = self.initials2()
self.salary = self.salary(self.age)
# using internal methods
def initials(self):
'''
long calculation
'''
return self.fathername[0:1]
def initials2(self):
'''
long calculation based on initials1 and on many previously calculated attributes
'''
return self.initials.upper()
@staticmethod
def salary(age):
'''
long calculation
'''
result = age*1000
return result
the question is what o the follwing is the way to go.
a) no methods at all,all the code together and when the attributes are calculated simply set them
# peseudocode:
self.attribute = XX
# further calculations
self.attribute2 = YY
b) As in the example code above. Using internal methods to separate functionalities. Including class methods that return a value that is passed to the corresponding attribute. The advantage is that it is possible to nicely arrange all the attributes in init gives a nice overview
c) using static methods. Which I think is not the way to go because they are never going to be called from outside the class
d) using @property. which I think is not the way to go since that would mean every time an attribute is called the methods rerun.
I would go for option b, but somehow I have the feeling is not very pythonic.
Which other way would code that class otherwise assuming the code in the methods is quite long?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为选项
b
是最好的选项,原因与您已经提到的相同。但是,如果你的计算很长,你可以尝试看看各个方法之间是否存在重复运算。通过这种方式,您可以创建临时函数来执行这些操作,从而减少其他操作的计算。不建议使用很长的方法,因为它们很难正确编写、难以理解和难以测试。I think option
b
is the best one for the same reasons you already mentioned. However, if your calculations are very long, you could try to see if there are repeated operations between the various methods. This way you could create ad hoc functions to perform those operations, thus reducing the calculations of the others. It is not recommended to have very long methods, as they are difficult to write correctly, difficult to understand and difficult to test.