我想知道__init__和__call__方法之间的区别。

例如:

class test:

  def __init__(self):
    self.a = 10

  def __call__(self): 
    b = 20

当前回答

__init__()可以:

初始化类的实例。 被叫了很多次。 只返回None。

__call__()可以像实例方法一样自由使用。

例如,Person类有__init__()和__call__(),如下所示:

class Person:
    def __init__(self, f_name, l_name):
        self.f_name = f_name
        self.l_name = l_name
        print('"__init__()" is called.')
        
    def __call__(self, arg):
        return arg + self.f_name + " " + self.l_name

现在,我们创建并初始化Person类的实例,如下所示:

    # Here
obj = Person("John", "Smith")

然后调用__init__(),如下所示:

"__init__()" is called.

接下来,我们以如下所示的2种方式调用__call__():

obj = Person("John", "Smith")
print(obj("Hello, ")) # Here
print(obj.__call__("Hello, ")) # Here

然后调用__call__(),如下所示:

"__init__()" is called.
Hello, John Smith # Here
Hello, John Smith # Here

并且,__init__()可以多次调用,如下所示:

obj = Person("John", "Smith")
print(obj.__init__("Tom", "Brown")) # Here
print(obj("Hello, "))
print(obj.__call__("Hello, "))

然后,调用__init__(), Person类的实例被重新初始化,并从__init__()返回None,如下所示:

"__init__()" is called.
"__init__()" is called. # Here
None # Here
Hello, Tom Brown
Hello, Tom Brown

并且,如果__init__()不返回None,我们调用__init__(),如下所示:

class Person:
    def __init__(self, f_name, l_name):
        self.f_name = f_name
        self.l_name = l_name
        print('"__init__()" is called.')
        return "Hello" # Here
        
    # ...

obj = Person("John", "Smith") # Here

出现以下错误:

__init__()应该返回None,而不是'str'

并且,如果Person类中没有定义__call__:

class Person:
    def __init__(self, f_name, l_name):
        self.f_name = f_name
        self.l_name = l_name
        print('"__init__()" is called.')
        
    # def __call__(self, arg):
    #     return arg + self.f_name + " " + self.l_name

然后,我们调用obj("Hello, "),如下所示:

obj = Person("John", "Smith")
obj("Hello, ") # Here

出现以下错误:

'Person'对象不可调用

同样,我们调用obj。__call__("Hello, ")如下所示:

obj = Person("John", "Smith")
obj.__call__("Hello, ") # Here

出现以下错误:

AttributeError: 'Person'对象没有属性'__call__'

其他回答

__init__()可以:

初始化类的实例。 被叫了很多次。 只返回None。

__call__()可以像实例方法一样自由使用。

例如,Person类有__init__()和__call__(),如下所示:

class Person:
    def __init__(self, f_name, l_name):
        self.f_name = f_name
        self.l_name = l_name
        print('"__init__()" is called.')
        
    def __call__(self, arg):
        return arg + self.f_name + " " + self.l_name

现在,我们创建并初始化Person类的实例,如下所示:

    # Here
obj = Person("John", "Smith")

然后调用__init__(),如下所示:

"__init__()" is called.

接下来,我们以如下所示的2种方式调用__call__():

obj = Person("John", "Smith")
print(obj("Hello, ")) # Here
print(obj.__call__("Hello, ")) # Here

然后调用__call__(),如下所示:

"__init__()" is called.
Hello, John Smith # Here
Hello, John Smith # Here

并且,__init__()可以多次调用,如下所示:

obj = Person("John", "Smith")
print(obj.__init__("Tom", "Brown")) # Here
print(obj("Hello, "))
print(obj.__call__("Hello, "))

然后,调用__init__(), Person类的实例被重新初始化,并从__init__()返回None,如下所示:

"__init__()" is called.
"__init__()" is called. # Here
None # Here
Hello, Tom Brown
Hello, Tom Brown

并且,如果__init__()不返回None,我们调用__init__(),如下所示:

class Person:
    def __init__(self, f_name, l_name):
        self.f_name = f_name
        self.l_name = l_name
        print('"__init__()" is called.')
        return "Hello" # Here
        
    # ...

obj = Person("John", "Smith") # Here

出现以下错误:

__init__()应该返回None,而不是'str'

并且,如果Person类中没有定义__call__:

class Person:
    def __init__(self, f_name, l_name):
        self.f_name = f_name
        self.l_name = l_name
        print('"__init__()" is called.')
        
    # def __call__(self, arg):
    #     return arg + self.f_name + " " + self.l_name

然后,我们调用obj("Hello, "),如下所示:

obj = Person("John", "Smith")
obj("Hello, ") # Here

出现以下错误:

'Person'对象不可调用

同样,我们调用obj。__call__("Hello, ")如下所示:

obj = Person("John", "Smith")
obj.__call__("Hello, ") # Here

出现以下错误:

AttributeError: 'Person'对象没有属性'__call__'

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

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

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

第二部分实现函数调用操作符。

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

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

方法用于使对象像函数一样工作。

>>> class A:
...     def __init__(self):
...         print "From init ... "
... 
>>> a = A()
From init ... 
>>> a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: A instance has no __call__ method

<*There is no __call__ method so it doesn't act like function and throws error.*>

>>> 
>>> class B:
...     def __init__(self):
...         print "From init ... "
...     def __call__(self):
...         print "From call it is a function ... "
... 
>>> b = B()
From init ... 
>>> b()
From call it is a function... 
>>> 

<* __call__ method made object "b" to act like function *>

我们也可以把它传递给一个类变量。

class B:
    a = A()
    def __init__(self):
       print "From init ... "
>>> class A:
...     def __init__(self):
...         print "From init ... "
... 
>>> a = A()
From init ... 
>>> a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: A instance has no __call__ method
>>> 
>>> class B:
...     def __init__(self):
...         print "From init ... "
...     def __call__(self):
...         print "From call ... "
... 
>>> b = B()
From init ... 
>>> b()
From call ... 
>>> 

案例1:

class Example:
    def __init__(self, a, b, c):
        self.a=a
        self.b=b
        self.c=c
        print("init", self.a, self.b, self.c)

Run:

Example(1,2,3)(7,8,9)

结果:

- init 1 2 3
- TypeError: 'Example' object is not callable

案例2:

class Example:
    def __init__(self, a, b, c):
        self.a=a
        self.b=b
        self.c=c
        print("init", self.a, self.b, self.c)
    def __call__(self, x, y, z):
        self.x=x
        self.y=y
        self.z=z
        print("call", self.x, self.y, self.z)

Run:

Example(1,2,3)(7,8,9)

结果:

- init 1 2 3
- call 7 8 9