【问题标题】:multiple argument values error多个参数值错误
【发布时间】:2018-04-06 12:43:02
【问题描述】:

我刚开始使用 Python 的 OOP(如果这是一个愚蠢的问题,请见谅)。我正在使用一个模型,该模型使用一个函数来模拟大气中二氧化碳排放的衰减。对于每个时间步,Emission 类的c_size 属性应该使用decay 函数减少

我已将代码简化为一次运行,但我不断收到错误:

Traceback (most recent call last):
File "<co2>", line 20, in <module>
x.decay(year=1) 
TypeError: decay() got multiple values for argument 'year'

我看不出任何多个值的来源。我只向代码传递了一个 int 值。

代码是:

import numpy as np

class Emission:
    def __init__(self,e_year=0, e_size=0.0):
        self.e_year=e_year #emission year
        self.e_size=e_size #emission size
        self.c_size=[e_size] #current size

    def decay(year):
        #returns a % decay based on the year since emission 0 = 100% 1 = 93%
        # etc. from AR5 WG1 Chapter 8 supplementary material equation 8.SM.10
        # using coefficients from table 8.SM.10
        term1 = 0.2173
        term2 = 0.224*np.exp(-year/394.4)
        term3 = 0.2824*np.exp(-year/36.54)
        term4 = 0.2763*np.exp(-year/4.304)
        out = (term1+term2+term3+term4)*self.e_size
        self.c_size.append(out)
        print(self.c_size)

x = Emission(e_year=1,e_size=10.0)
x.decay(year=1)

【问题讨论】:

  • FWIW,一旦你将self 添加到Emission.decay 的方法签名中,你的代码就会打印出[10.0, 9.3452514335147363]。你可能想知道为什么 Python 没有在这里给出更有用的错误信息。好吧,它不能——self 这个名字没有什么特别之处,它只是一个用来指代实例的传统名称,Python 允许你随意命名它。
  • 谢谢 - 这正是我所期待的。它仍然是 WIP,但我需要能够显示每个对象随时间衰减的历史 - 因此需要一个列表

标签: python python-3.x function class oop


【解决方案1】:

您忘记了 decay 的方法签名中的 self,因此 Python 尝试将实例作为第一个参数(即 year)插入,并且您还显式传入了 year - 导致参数冲突.

所以只需将方法定义更改为:

def decay(self, year):

重要的是要意识到方法的第一个参数的名称self 只是一个约定。如果您将其命名为year(尽管您必须为第二个参数找到不同的名称),或者thisthe_instance,它也可以工作。重要的是,当您调用方法时,实例会作为第一个参数隐式传递给方法。

所以这也可以:

def decay(blabla, year):
    term1 = 0.2173
    term2 = 0.224*np.exp(-year/394.4)
    term3 = 0.2824*np.exp(-year/36.54)
    term4 = 0.2763*np.exp(-year/4.304)
    out = (term1+term2+term3+term4)*blabla.e_size
    blabla.c_size.append(out)
    print(blabla.c_size)

但这违反了常见的 Python 约定,我不得不将方法中的所有 selfs 更改为 blabla


更多内容:

如果您专门以year 处理标量,您可能应该使用math.exp 而不是numpy.exp,因为它要快得多。但是math.exp 只适用于标量,所以如果你想为year 使用列表或数组,你必须使用numpy.exp

【讨论】:

  • 感谢您的帮助!我很欣赏一个非常简单的问题没有嘲笑:)
  • @WillRolls 是的,这很简单,但我们都曾有过这样或那样的经历,即使是我们这些有很多 OOP 经验的人。
【解决方案2】:

类方法的第一个参数应该是self。

class Emission:
    def __init__(self,e_year=0, e_size=0.0):
        self.e_year=e_year #emission year
        self.e_size=e_size #emission size
        self.c_size=[e_size] #current size

    def decay(self, year):
        #returns a % decay based on the year since emission 0 = 100% 1 = 93% etc. from AR5 WG1 Chapter 8 supplementary material equation 8.SM.10 using coefficients from table 8.SM.10
        term1 = 0.2173
        term2 = 0.224*np.exp(-year/394.4)
        term3 = 0.2824*np.exp(-year/36.54)
        term4 = 0.2763*np.exp(-year/4.304)
        out = (term1+term2+term3+term4)*self.e_size
        self.c_size.append(out)
        print(self.c_size)

【讨论】:

  • 谢谢,我认为这很简单(对于知道自己在做什么的人......)
猜你喜欢
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 2017-05-30
相关资源
最近更新 更多