根据您所写的内容,您缺少一个关键的理解:类和对象之间的区别。 __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.legs 是变量self 中对象的一个名为legs 的属性。属性有点像变量,但它们描述了对象的状态,或对象可用的特定操作(功能)。
但是,请注意,您没有为 doghood 本身设置 colour - 这是一个抽象概念。有些属性对类有意义。例如,population_size 就是其中之一 - 计算 Fido 是没有意义的,因为 Fido 始终是一个。数狗确实有意义。假设世界上有 2 亿只狗。它是 Dog 类的属性。 Fido 与 2 亿这个数字无关,Spot 也没有。它被称为“类属性”,而不是上面的colour 或legs 的“实例属性”。
现在,到一些更少的狗和更多与编程相关的东西。正如我在下面写的,添加东西的类是不明智的——它是什么类? 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 变量,在不同的狗上。所以,fido.colour 是一个变量,而spot.colour 是另一个变量。第一个绑定到变量fido内的对象;第二个,spot。现在,当您调用Dog(4, "brown") 或three.add(five) 时,总会有一个不可见的参数,它会分配给参数列表前面的悬空额外参数。它通常称为self,并将获取点前面的对象的值。因此,在 Dog 的 __init__(构造函数)中,self 将是新 Dog 的任何结果;在MyInteger 的add 内,self 将绑定到变量three 中的对象。因此,three.value 将与add 外部的变量相同,与add 内部的self.value 相同。
如果我说the_mangy_one = fido,我将开始使用另一个名称来引用称为fido 的对象。从现在开始,fido.colour 与the_mangy_one.colour 完全相同。
所以,__init__ 里面的东西。您可以将它们视为在狗的出生证明中记录的内容。 colour 本身是一个随机变量,可以包含任何内容。 fido.colour 或 self.colour 就像 Dog 身份表上的表单域;而__init__是第一次填写的店员。
有更清楚的吗?
编辑:扩展下面的评论:
您的意思是对象的列表,不是吗?
首先,fido 实际上不是一个对象。它是一个变量,当前包含一个对象,就像你说x = 5,x 是一个当前包含数字五的变量。如果你后来改变主意,你可以做fido = Cat(4, "pleasing")(只要你已经创建了一个类Cat),然后fido 将从此“包含”一个猫对象。如果你输入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__方法来返回这个名字。这个方法(即类绑定函数,就像add 或__init__)的目的是描述如何将对象转换为字符串,就像打印出来一样。