from_的继承在Python中
编辑:有一些困惑,但我想问一个关于Python中面向对象设计的一般问题。
考虑一个可以让您将数据值映射到计数或频率的类:
class DataMap(dict):
pass
现在考虑一个子类,它允许您从数据列表构建直方图:
class Histogram(DataMap):
def __init__(self, list_of_values):
# 1. Put appropriate super(...) call here if necessary
# 2. Build the map of values to counts in self
pass
现在考虑一个类,它可以让您制作平滑的概率质量表而不是直方图。
class ProbabilityMass(DataMap):
pass
允许从直方图
或值列表构造ProbabilityMass
的最佳方法是什么?
我是在 C++ 中“长大”的,在这种情况下,我会使用重载的构造函数。在Python中,我考虑过这样做:
- 构造函数采用多个参数(除了其中一个之外的所有参数都应该==无)
- 我定义了 from_Histogram 和 from_list 方法
在第二种情况下(我认为更好),最好的方法是什么允许 from_list
方法使用 Histogram
构造函数中的共享代码? ProbabilityMass
表与 Histogram
表几乎相同,但它经过缩放,以便所有值的总和为 1.0。
如果您遇到过类似问题,请分享您的专业知识!
Edit: There was some confusion, but I want to ask a general question about object oriented design in Python.
Consider a class that lets you map data values to counts or frequencies:
class DataMap(dict):
pass
Now consider a subclass that allows you to construct a histogram from a list of data:
class Histogram(DataMap):
def __init__(self, list_of_values):
# 1. Put appropriate super(...) call here if necessary
# 2. Build the map of values to counts in self
pass
Now consider a class that lets you make a smoothed probability mass table rather than a Histogram
.
class ProbabilityMass(DataMap):
pass
What is the best way to allow a ProbabilityMass
to be constructed from either a Histogram
or a list of values?
I "grew up" programming in C++, and in this case I would use an overloaded constructor. In Python I've thought of doing this with:
- The constructor takes multiple arguments (all but one of these should == None)
- I define from_Histogram and from_list methods
In the second case (which I believe is better), what is the best way to allow the from_list
method to use the shared code from the Histogram
constructor? A ProbabilityMass
table is nearly identical to a Histogram
table, but it is scaled so that the sum of all value is 1.0.
If you have come across a similar problem, please share your expertise!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果您认为需要
@staticmethod
,那么您几乎总是不需要。该函数要么不是类的一部分,在这种情况下它应该只是一个自由函数,要么它是类的一部分,但不绑定到实例,并且它应该是一个@classmethod
。您的命名构造函数是@classmethod
的良好候选者。另请注意,您应该通过
super()
从B
调用A.__init__
,否则多重继承会给您带来不好的影响。在这种情况下,您不需要
B.from_values_to_counts
,它继承自A
,并且它将返回B
的实例,因为这就是它的名字。如果您需要在
B
中进行更复杂的初始化,可以使用super()
,它看起来与在实例中使用它时的方式非常相似。毕竟,classmethod
实际上并不比instancemethod
更复杂,其中im_self
属性被分配给类本身。To start with, if you think you want
@staticmethod
, you almost always don't. Either the function is not part of the class, in which case it should just be a free function, or it is part of the class, but not tied to an instance, and it should be a@classmethod
. Your named constructor is a good candidate for a@classmethod
.Also note that you should invoke
A.__init__
fromB
viasuper()
, otherwise multiple inheritance can bite you bad.In this case, you don't need a
B.from_values_to_counts
, it inherits fromA
, and it will return an instance ofB
, since that's how it was called.If you need to do more complex initialization in
B
, you can, usingsuper()
, which looks very similar to the way it would when you use it with instances. after all, aclassmethod
really isn't anything more complex than aninstancemethod
where theim_self
attribute is assigned to the class itself.