列表中的python self关键字

纳瑟·皮尔坦

我正在阅读有关keras的教程,并且遇到了由其他类继承的此类。

  class LearningRateDecay:
  def plot(self, epochs, title="Learning Rate Schedule"):
    lrs = [self(i) for i in epochs]     
    plt.style.use("ggplot")
    plt.figure()
    plt.plot(epochs, lrs)
    plt.title(title)
    plt.xlabel("Epoch #")
    plt.ylabel("Learning Rate")

在其中这样定义时代:

    N = 100
    epochs = np.arange(0, N)

并传递给绘图函数,如下所示:

    a = LearningRateDecay
    a.plot(epochs,"Learning Rate Schedule")

我无法理解self(i)是什么意思?这是关于访问自我元素还是其他东西?任何帮助是极大的赞赏。谢谢

亚瑟·许

self(i)指调用对象,即使用其__call__方法。

例如,考虑此类:

class MyClass:
    def __call__(self, arg):
        print(arg)
    def method(self):
        self('test')

a = MyClass()
a.method()  # Prints 'test'
a('other')  # Prints 'other'

在您的示例中,此类可能具有与之关联的某些已定义行为__call__(也可以从上层对象继承)。请参阅课程文档以了解有关信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章