Warm tip: This article is reproduced from serverfault.com, please click

class-为什么我们在 Python 类中使用 __init__?

(class - Why do we use __init__ in Python classes?)

发布于 2011-12-22 20:06:39

我无法理解类的初始化。

它们的重点是什么,我们如何知道要包含在其中的内容?在类中编写与创建函数是否需要不同类型的思维(我认为我可以创建函数然后将它们包装在一个类中以便我可以重用它们。这行得通吗?)

下面是一个例子:

class crawler:
  # Initialize the crawler with the name of database
  def __init__(self,dbname):
    self.con=sqlite.connect(dbname)

  def __del__(self):
    self.con.close()

  def dbcommit(self):
    self.con.commit()

或另一个代码示例:

class bicluster:
  def __init__(self,vec,left=None,right=None,distance=0.0,id=None):
    self.left=left
    self.right=right
    self.vec=vec
    self.id=id
    self.distance=distance

__init__在尝试阅读其他人的代码时我遇到了很多类,但我不明白创建它们的逻辑。

Questioner
Lostsoul
Viewed
33
12.1k 2021-06-19 19:46:15

根据你写的内容,你错过了一个关键的理解:类和对象之间的区别。__init__不初始化一个类,它初始化一个类或一个对象的实例。每条狗都有颜色,但狗作为一个类没有。每只狗都有四只或更少的脚,但狗类没有。类是对象的概念。当你看到 Fido 和 Spot 时,你会认识到他们的相似之处,他们的狗血统。这就是班级。

当你说

class Dog:
    def __init__(self, legs, colour):
        self.legs = legs
        self.colour = colour

fido = Dog(4, "brown")
spot = Dog(3, "mostly yellow")

你是说,Fido 是一只有 4 条腿的棕色狗,而 Spot 有点跛子,大部分是黄色的。__init__函数称为构造函数或初始值设定项,并在你创建类的新实例时自动调用。在该函数中,新创建的对象被分配给参数self符号self.legslegs变量中对象的一个属性self属性有点像变量,但它们描述对象的状态,或对象可用的特定操作(函数)。

但是,请注意,你并没有设置colourdoghood 本身——它是一个抽象的概念。有些属性对类有意义。例如,population_size是这样的 - 计算 Fido 没有意义,因为 Fido 总是一个。数狗确实有意义。假设世界上有 2 亿只狗。它是 Dog 类的属性。Fido 与 2 亿这个数字无关,Spot 也没有。它被称为“类属性”,而不是等于colourlegs高于“实例属性”

现在,少一些犬类和更多与编程相关的东西。正如我在下面写的,添加东西的类是不明智的 - 它是什么类?Python 中的类由不同数据的集合组成,它们的行为相似。狗类包括 Fido 和 Spot 以及 199999999998 其他与它们相似的动物,它们都在灯柱上撒尿。添加东西的类由什么组成?它们的不同之处在于它们固有的哪些数据?他们分享什么行动?

然而,数字……那些是更有趣的主题。说,整数。他们有很多,比狗多得多。我知道 Python 已经有了整数,但让我们装傻再“实现”它们(通过欺骗和使用 Python 的整数)。

所以,整数是一个类。他们有一些数据(值)和一些行为(“将我添加到另一个数字”)。让我们展示一下:

class MyInteger:
    def __init__(self, newvalue):
        # imagine self as an index card.
        # under the heading of "value", we will write
        # the contents of the variable newvalue.
        self.value = newvalue
    def add(self, other):
        # when an integer wants to add itself to another integer,
        # we'll take their values and add them together,
        # then make a new integer with the result value.
        return MyInteger(self.value + other.value)

three = MyInteger(3)
# three now contains an object of class MyInteger
# three.value is now 3
five = MyInteger(5)
# five now contains an object of class MyInteger
# five.value is now 5
eight = three.add(five)
# here, we invoked the three's behaviour of adding another integer
# now, eight.value is three.value + five.value = 3 + 5 = 8
print eight.value
# ==> 8

这有点脆弱(我们假设other将是一个 MyInteger),但我们现在将忽略。在实际代码中,我们不会;我们会测试它以确保,甚至可能会强制它(“你不是一个整数?天哪,你有 10 纳秒变成一个!9 ... 8 ....”)

我们甚至可以定义分数。分数也知道如何添加自己。

class MyFraction:
    def __init__(self, newnumerator, newdenominator):
        self.numerator = newnumerator
        self.denominator = newdenominator
        # because every fraction is described by these two things
    def add(self, other):
        newdenominator = self.denominator * other.denominator
        newnumerator = self.numerator * other.denominator + self.denominator * other.numerator
        return MyFraction(newnumerator, newdenominator)

分数甚至比整数还多(不是真的,但计算机不知道)。让我们做两个:

half = MyFraction(1, 2)
third = MyFraction(1, 3)
five_sixths = half.add(third)
print five_sixths.numerator
# ==> 5
print five_sixths.denominator
# ==> 6

你实际上并没有在这里声明任何东西。属性就像一种新的变量。正常变量只有一个值。让我们说你写colour = "grey"你不能有一个名为另一个变量colour"fuchsia"-并非在代码相同的地方。

数组在一定程度上解决了这个问题。如果你说colour = ["grey", "fuchsia"],你已将两种颜色堆叠到变量中,但你可以通过它们的位置(在本例中为 0 或 1)来区分它们。

属性是绑定到对象的变量。就像数组一样,我们可以在不同的狗上有很多colour变量所以,是一个变量,但又是另一个。第一个绑定到变量内的对象第二个,现在,当你调用, or 时,总会有一个不可见的参数,该参数将分配给参数列表前面的悬空额外参数。它通常被称为,并将获取点前面的对象的值。因此,在 Dog 的(构造函数)中,将是新的 Dog 将变成的任何内容;,将绑定到变量中的对象因此,fido.colourspot.colourfidospotDog(4, "brown")three.add(five)self__init__selfMyIntegeraddselfthreethree.value将是 . 之外的相同变量addself.valueadd.

如果我说the_mangy_one = fido,我将开始fido用另一个名字来指代已知的对象从现在开始,fido.colour与 完全相同的变量the_mangy_one.colour

所以,里面的东西__init__你可以把它们看作是在狗的出生证明上记下的东西。colour本身是一个随机变量,可以包含任何东西。fido.colour或者self.colour就像狗的身份证上的表单字段;并且__init__是店员第一次填写。

有更清楚的吗?

编辑:扩展下面的评论:

你的意思是一个对象列表,不是吗?

首先,fido实际上不是一个对象。它是一个变量,当前包含一个对象,就像你说的那样x = 5x是一个当前包含数字 5 的变量。如果你后来改变主意,你可以这样做fido = Cat(4, "pleasing")(只要你已经创建了一个类Cat),并且fido从那时起“包含”一个 cat 对象。如果这样做fido = x,它将包含数字 5,而根本不是动物对象。

除非你专门编写代码来跟踪它们,否则类本身并不知道其实例。例如:

class Cat:
    census = [] #define census array

    def __init__(self, legs, colour):
        self.colour = colour
        self.legs = legs
        Cat.census.append(self)

这里,census是类的类级属性Cat

fluffy = Cat(4, "white")
spark = Cat(4, "fiery")
Cat.census
# ==> [<__main__.Cat instance at 0x108982cb0>, <__main__.Cat instance at 0x108982e18>]
# or something like that

请注意,你不会得到[fluffy, sparky]. 这些只是变量名。如果你想让猫自己有名字,你必须为名字做一个单独的属性,然后重写__str__方法来返回这个名字。这个方法(即类绑定函数,就像addor一样__init__)的目的是描述如何将对象转换为字符串,就像打印出来一样。