抽象类中的过载
我试图弄清楚如何将__添加__
方法在python中作为抽象类,但无法。这是我要解决的问题:
import numpy as np
from abc import ABC, abstractmethod
class RandomVariable(ABC):
@abstractmethod
def sample(self):
pass
# I want to override the __add__ method
# to return a random variable
# such that the sample method is the addition from
# left.sample() and right.sample()
class NormalVariable(RandomVariable):
def __init__(self, mu=0, sigma=1):
self.mu = mu
self.sigma = sigma
def sample(self):
return np.random.normal(self.mu, self.sigma)
class ExponentialVariable(RandomVariable):
def __init__(self, scale=1):
self.scale=scale
def sample(self):
return np.random.exponential(self.scale)
也就是说,我希望能够执行my_random_variable = normalVariable() + exponentialVariable()
。
编辑
这是解决它的标准方法:
import numpy as np
from abc import ABC, abstractmethod
class RandomVariable(ABC):
@abstractmethod
def sample(self):
pass
def __add__(self, other):
return AddedVariable(left=self, right=other)
class NormalVariable(RandomVariable):
def __init__(self, mu=0, sigma=1):
self.mu = mu
self.sigma = sigma
def sample(self):
return np.random.normal(self.mu, self.sigma)
class ExponentialVariable(RandomVariable):
def __init__(self, scale=1):
self.scale=scale
def sample(self):
return np.random.exponential(self.scale)
class AddedVariable(RandomVariable):
def __init__(self, left, right):
self.left = left
self.right = right
def sample(self):
return self.left.sample() + self.right.sample()
I was trying to figure out how to overload the __add__
method in python for an abstract class, but was unable to. Here is the problem I am trying to solve:
import numpy as np
from abc import ABC, abstractmethod
class RandomVariable(ABC):
@abstractmethod
def sample(self):
pass
# I want to override the __add__ method
# to return a random variable
# such that the sample method is the addition from
# left.sample() and right.sample()
class NormalVariable(RandomVariable):
def __init__(self, mu=0, sigma=1):
self.mu = mu
self.sigma = sigma
def sample(self):
return np.random.normal(self.mu, self.sigma)
class ExponentialVariable(RandomVariable):
def __init__(self, scale=1):
self.scale=scale
def sample(self):
return np.random.exponential(self.scale)
That is, I want to be able to do something like my_random_variable = NormalVariable() + ExponentialVariable()
.
Edit
Is this the standard way of solving it:
import numpy as np
from abc import ABC, abstractmethod
class RandomVariable(ABC):
@abstractmethod
def sample(self):
pass
def __add__(self, other):
return AddedVariable(left=self, right=other)
class NormalVariable(RandomVariable):
def __init__(self, mu=0, sigma=1):
self.mu = mu
self.sigma = sigma
def sample(self):
return np.random.normal(self.mu, self.sigma)
class ExponentialVariable(RandomVariable):
def __init__(self, scale=1):
self.scale=scale
def sample(self):
return np.random.exponential(self.scale)
class AddedVariable(RandomVariable):
def __init__(self, left, right):
self.left = left
self.right = right
def sample(self):
return self.left.sample() + self.right.sample()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论