简体   繁体   English

在不定义__init__函数的情况下创建类的实例

[英]Creating an instance of a class without defining the __init__ function

I am relativly new to python and I was wondering if you could create an instance of a class without defining the init explicity. 我相对来说是python的新手,我想知道你是否可以在不定义init明确的情况下创建一个类的实例。 Could I call it something else? 我可以称之为其他吗?

First example - with the init method: 第一个例子 - 使用init方法:

class dog:
def __init__(self,name):
    self.name=name
    print('My name is',name)
Bob = dog('Bob')

Second example - without the init method: 第二个例子 - 没有init方法:

class dog:
def init_instance(self,name):
    self.name = name
    print('My name is',name)

Bob = dog('Bob')

In the first example the code works but in the second example I get: 在第一个例子中,代码有效,但在第二个例子中,我得到:

TypeError: object() takes no parameters TypeError:object()不带参数

So based on this I assume that one has to explicitly call the init method. 所以基于此我假设必须显式调用init方法。 BUT I have seen code where the init method has not been used, how come? 但是我看过没有使用过init方法的代码,为什么会这样?

Every class has an __init__ method. 每个类都有__init__方法。 If it doesn't explicitly define one, then it will inherit one from its parent class. 如果它没有明确定义一个,那么它将从其父类继承一个。 In your 2nd example, the class inherits __init__ and a bunch of other methods (and other non-method attributes) from the base object class. 在第二个示例中,该类从基础object类继承__init__和一堆其他方法(以及其他非方法属性)。 We can see that via the dir function: 我们可以通过dir函数看到:

class Dog:
    def init_instance(self,name):
        self.name = name
        print('My name is',name)

print(dir(Dog))

output 产量

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'init_instance']

__init__ gets called automatically after the instance is constructed (via the __new__ method), so we might as well use it if we need to initialize our instance. 在构造实例之后(通过__new__方法)自动调用__init__ ,因此如果我们需要初始化实例,我们也可以使用它。 But we can call your init_instance explicitly: 但是我们可以显式调用你的init_instance

bob = Dog()
bob.init_instance('Bob')
print(bob.name)    

output 产量

My name is Bob
Bob

If you give you class an initializer that isn't named __init__ then it won't get called automatically. 如果你为类提供一个名为__init__的初始化程序,那么它将不会被自动调用。 How should Python know that that method is an initializer? Python应该如何知道该方法是初始化器? Although it's customary to make __init__ the first method in the class definition, that's by no means mandatory, and some people like to put __init__ last . 尽管习惯__init__作为类定义中的第一个方法,但这绝不是强制性的,有些人喜欢将__init__放在最后

You said: "I have seen code where the init method has not been used, how come?" 你说:“我看过没有使用过init方法的代码,为什么会这样?” Well, some classes simply don't need their instances to be initialized: their instance attributes are set via various other methods, or by direct assignment in code outside the class definition, eg bob.color = 'brown' . 好吧,有些类根本不需要初始化它们的实例:它们的实例属性是通过各种其他方法设置的,或者通过在类定义之外的代码中直接赋值,例如bob.color = 'brown' Or they inherit a perfectly usable __init__ from a parent class. 或者他们从父类继承了一个完全可用的__init__

init is nothing else then a method to initially prepare the state of your object. init就是最初准备对象状态的方法。 In other languages they have similar concepts as Constructors and it's not necessarily needed. 在其他语言中,它们具有与Constructors类似的概念,并不一定需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用和不使用 __init__() 的正确参数创建类的实例有什么区别? - What is the difference creating an instance of a class with and without the correct parameter of __init__()? 创建不直接访问父类__init __()函数的子类 - Creating a subclass without direct access to the parent class __init__() function 在定义类时,在__init __(self)函数中理解实例对象引用自约定 - understanding instance object in reference to self convention in __init__(self) function when defining class 如何在不定义__init__的情况下知道实例变量? - How does Python know the instance variable without defining __init__? python3类__init__继承而无需再次定义参数 - python3 class __init__ inheritance without defining parameters again class __init __(不是实例__init__) - class __init__ (not instance __init__) 调用类函数而不触发 __init__ - Calling a class function without triggering the __init__ 在类的__init__函数中定义numpy.array - Defining numpy.array in the __init__ function of a class 在不调用__init__的情况下动态创建类型(self)的实例? - Creating an instance of type(self) dynamically without calling __init__? 在__init__中使用函数和实例方法定义实例变量 - Defining instance variables using functions and instance methods in __init__
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM