抽象类中的过载

发布于 2025-02-06 15:23:31 字数 1783 浏览 1 评论 0原文

我试图弄清楚如何将__添加__方法在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文