Python 方法内所有实例的一个属性的总和

发布于 2025-01-11 20:07:44 字数 1445 浏览 1 评论 0原文

所以我有这个类:(

我之前有一些变量)

class Phases:
    """Phases properties"""

    def __init__(self, name, volume, density, foc):
        """initialize phase properties"""

        self.name = name
        self.volume = volume  # m^3
        self.density = density  # Kg/dm^3
        self.foc = foc
        if name == "acquatic biota ":
            self.kp = pow(10, logkbio)
            self.zvalue = self.kp * density / h
        else:
            self.kp = koc * foc
            if name == "air ":
                self.zvalue = 1 / (r * t)
            elif name == "water ":
                self.zvalue = 1 / h
            else:
                self.zvalue = self.kp * density / h
        self.zivi = self.zvalue * volume



air = Phases("air ", 24000000000000, 0.00119, 0)
water = Phases("water ", 1200000000, 1, 0)
soil = Phases("soil ", 2280000000, 1.5, 0.02)
sed = Phases("sediment ", 24000000, 1.5, 0.04)
susp = Phases("suspended solids ", 12000, 1.5, 0.04)
bio = Phases("acquatic biota ", 1200, 1, 0)

所以,问题是我想要一个名为 sumzivi 的新变量,它代表 sum (air.zivi、water.zivi、soil.zivi、sed.zivi、 susp.zivi,bio.zivi)

我尝试创建一个新列表,但如果我创建该列表,该列表位于我的对象定义之后。我需要将总和放在方法内部,因为在总和本身之后,我需要放置另一个使用 sumzivi 和其他属性的属性以前的属性(像这样:
self.mol = sumzivi * self.volume / h

可能吗?或者您是否建议不要使用类并以不同的方式管理这些数据?

非常感谢,对虚拟编码感到抱歉,但我是一名第一次接触编码的环境科学家......

我必须创建许多像这样的方法,所以我正在寻找最简单和最快的方法来做到这一点,谢谢<3

so i have this class:

(i have some variables before)

class Phases:
    """Phases properties"""

    def __init__(self, name, volume, density, foc):
        """initialize phase properties"""

        self.name = name
        self.volume = volume  # m^3
        self.density = density  # Kg/dm^3
        self.foc = foc
        if name == "acquatic biota ":
            self.kp = pow(10, logkbio)
            self.zvalue = self.kp * density / h
        else:
            self.kp = koc * foc
            if name == "air ":
                self.zvalue = 1 / (r * t)
            elif name == "water ":
                self.zvalue = 1 / h
            else:
                self.zvalue = self.kp * density / h
        self.zivi = self.zvalue * volume



air = Phases("air ", 24000000000000, 0.00119, 0)
water = Phases("water ", 1200000000, 1, 0)
soil = Phases("soil ", 2280000000, 1.5, 0.02)
sed = Phases("sediment ", 24000000, 1.5, 0.04)
susp = Phases("suspended solids ", 12000, 1.5, 0.04)
bio = Phases("acquatic biota ", 1200, 1, 0)

So, the problem is that i want a new variable called sumzivi that represent the sum (air.zivi, water.zivi, soil.zivi, sed.zivi, susp.zivi, bio.zivi).

I tried making a new list but if I do the list, the list is after my object definement.. i need the sum to be inside the method because after the sum itself, i need to put another attribute that uses the sumzivi and also other previous attributes (like this:
self.mol = sumzivi * self.volume / h)

is it possible? Or do you suggest not to use classes and to manage these data in a different way?

Thank you so much, sorry for the dummy coding but i'm an environmental scientist approaching coding for the first times...

I have to create many methods like this one so i'm searching for the easiest and fastest way to do it, thank you <3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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