直接调用__init__的目的是什么?

时间:2021-11-23 18:21:49

I am having a hard time figuring out the purpose some code that I've come across.

我很难弄清楚我遇到的一些代码的目的。

The code has a class Foo, which has an __init__ method that takes multiple arguments. From what I've learned of Python so far, by calling Foo('bar'), it will pass this string as a parameter to __init__ (which I think is supposed to be the equivalent of a constructor).

代码有一个类Foo,它有一个带有多个参数的__init__方法。从我到目前为止学到的Python,通过调用Foo('bar'),它将这个字符串作为参数传递给__init__(我认为它应该相当于构造函数)。

But the issue I am having is that the code I am looking at is calling Foo.__init__('bar') directly. What is the purpose of this? I almost feel that I am missing some other purpose behind __init__.

但我遇到的问题是我正在看的代码是直接调用Foo .__ init __('bar')。这样做的目的是什么?我几乎觉得我错过了__init__背后的其他目的。

3 个解决方案

#1


13  

The __init__() method gets called for you when you instantiate a class. However, the __init__() method in a parent class doesn't get called automatically, so need you to call it directly if you want to extend its functionality:

实例化一个类时,会调用__init __()方法。但是,父类中的__init __()方法不会自动调用,因此如果要扩展其功能,需要直接调用它:

class A:

     def __init__(self, x):
          self.x = x

class B(A):

     def __init__(self, x, y):
          A.__init__(self, x)
          self.y = y

Note, the above call can also be written using super:

注意,上面的调用也可以使用super编写:

class B(A):

     def __init__(self, x, y):
          super().__init__(x)
          self.y = y

The purpose of the __init__() method is to initialize the class. It is usually responsible for populating the instance variables. Because of this, you want to have __init__() get called for all classes in the class hierarchy.

__init __()方法的目的是初始化类。它通常负责填充实例变量。因此,您希望为类层次结构中的所有类调用__init __()。

#2


1  

Python allows you to call the constructor (__init__) directly. By calling Foo.__init__(obj, 'bar'), you're doing an initialization/reinitialization of obj

Python允许您直接调用构造函数(__init__)。通过调用Foo .__ init __(obj,'bar'),您正在进行obj的初始化/重新初始化

See this code:

看到这段代码:

class Foo:
    def __init__(self, s):
        self.s = s

f = Foo('abc')
print(f.s) # prints 'abc'
Foo.__init__(f, 'def')
print(f.s) # prints 'def'

#3


0  

Yes - when inheriting class invokes __init__() of the parent class. AFAIK, this is the only valid case of calling __init__() explicitly.

是 - 继承类时调用父类的__init __()。 AFAIK,这是明确调用__init __()的唯一有效案例。

#1


13  

The __init__() method gets called for you when you instantiate a class. However, the __init__() method in a parent class doesn't get called automatically, so need you to call it directly if you want to extend its functionality:

实例化一个类时,会调用__init __()方法。但是,父类中的__init __()方法不会自动调用,因此如果要扩展其功能,需要直接调用它:

class A:

     def __init__(self, x):
          self.x = x

class B(A):

     def __init__(self, x, y):
          A.__init__(self, x)
          self.y = y

Note, the above call can also be written using super:

注意,上面的调用也可以使用super编写:

class B(A):

     def __init__(self, x, y):
          super().__init__(x)
          self.y = y

The purpose of the __init__() method is to initialize the class. It is usually responsible for populating the instance variables. Because of this, you want to have __init__() get called for all classes in the class hierarchy.

__init __()方法的目的是初始化类。它通常负责填充实例变量。因此,您希望为类层次结构中的所有类调用__init __()。

#2


1  

Python allows you to call the constructor (__init__) directly. By calling Foo.__init__(obj, 'bar'), you're doing an initialization/reinitialization of obj

Python允许您直接调用构造函数(__init__)。通过调用Foo .__ init __(obj,'bar'),您正在进行obj的初始化/重新初始化

See this code:

看到这段代码:

class Foo:
    def __init__(self, s):
        self.s = s

f = Foo('abc')
print(f.s) # prints 'abc'
Foo.__init__(f, 'def')
print(f.s) # prints 'def'

#3


0  

Yes - when inheriting class invokes __init__() of the parent class. AFAIK, this is the only valid case of calling __init__() explicitly.

是 - 继承类时调用父类的__init __()。 AFAIK,这是明确调用__init __()的唯一有效案例。