__init__ 和 __call__ 有什么区别? [英] What is the difference between __init__ and __call__?

查看:37
本文介绍了__init__ 和 __call__ 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 __init____call__ 方法之间的区别.

I want to know the difference between __init__ and __call__ methods.

例如:

class test:

  def __init__(self):
    self.a = 10

  def __call__(self): 
    b = 20

推荐答案

第一个用于初始化新创建的对象,并接收用于执行此操作的参数:

The first is used to initialise newly created object, and receives arguments used to do that:

class Foo:
    def __init__(self, a, b, c):
        # ...

x = Foo(1, 2, 3) # __init__

第二个实现函数调用运算符.

The second implements function call operator.

class Foo:
    def __call__(self, a, b, c):
        # ...

x = Foo()
x(1, 2, 3) # __call__

这篇关于__init__ 和 __call__ 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆