何时创建 "instance"?
When is an "instance" being created?
我一直在阅读有关 python 的 OOP 文章,特别是 this one。
那篇文章的作者有描述和代码示例:
The Python syntax to instantiate a class is the same of a function
call
>>> b = int()
>>> type(b)
<type 'int'>
据此我推断 "instance" 在执行时存在,而不是之前。当您执行 type(b)
时,这是 class int().
的实例
但后来我读到了这个stack overflow answer:
Instance is a variable that holds the memory address of the Object.
这让我对这个词有点困惑。因此,当我在执行时分配一个变量时,会创建 "instance" 吗?
最后这个explanation in ComputerHope指出实例与变量赋值相同的事实:
function Animal(numlegs, mysound) {
this.legs = numlegs;
this.sound = mysound;
}
var lion = new Animal(4, "roar");
var cat = new Animal(4, "meow");
var dog = new Animal(4, "bark");
The Animal object allows for the number of legs and the sound the
animal makes to be set by each instance of the object. In this case,
all three instances (lion, cat, and dog) have the same number of legs,
but make different sounds.
谁能真正提供一个实例何时退出的明确定义?
通常在 OOP 中
Classes and objects are the two main aspects of object oriented
programming. A class creates a new type where objects are instances of
the class.
正如解释的那样here。
因此每次创建对象时,它都称为实例。
Python 在这个概念上没有区别,但是事情与其他语言有点不同,例如 Java。
事实上 Python 一切 都是对象,甚至 class 本身也是对象。
以下是其工作原理的简要说明:
考虑这个片段:
>>> class Foo:
... pass
...
>>> type(Foo)
<type 'type'>
>>>
Class Foo
是 type
类型,它是 Python 中所有 classes
的元 class
(然而 'old' 和 'new' class 之间有区别,更多 here, here and here)。
Class type
是一个 class,是其自身的一个实例:
>>> isinstance(type, type)
True
所以 Foo
尽管是 class 定义,但被解释器当作对象对待。
作为实例的对象是通过像 foo = Foo()
这样的语句创建的,foo
作为对象继承自 object
class.
>>> isinstance(foo, object)
True
这个class提供了对象需要的所有方法,例如__new__()
和__int__()
(new, init)。简而言之,前者用于创建 class 的新实例,后者在创建实例后调用,用于初始化值,就像您对 Animal
.
所做的那样
一切皆对象这一事实也意味着我们可以编写如下有趣的代码:
>>> class Foo:
... var = 'hello'
...
>>> foo = Foo()
>>> foo.var
'hello'
>>> foo.other_var = 'world'
>>> foo.other_var
'world'
>>> Foo.other_var
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Foo' has no attribute 'other_var'
>>> Foo.var
'hello'
>>>
这里我在运行时在对象上添加了一个属性。该属性将在 foo
中唯一存在,class 本身或任何其他实例都不会拥有它。
这叫做Instance variable and class variable.
希望这一切对你有意义。
TL;DR
在Python中,一切(class定义、functions、模块等)都被解释器视为对象。因此 'everything' 是一个实例。
I've been reading articles about OOP with python, specifically this
one.
The autor of that article has a description and then a code example:
The Python syntax to instantiate a class is the same of a function
call
>>> b = int()
>>> type(b)
<type 'int'>
再读一遍前面的句子:
Once you have a class you can instantiate it to get a concrete object (an instance) of that type, i.e. an object built according to the structure of that class.
所以 class 的一个实例是一个 object,其类型是 class。
By this I infer "instance" exist at the moment of the execution and
not before.
是的,正确。 "Instance" 和 "instance of" 是 Python.
中的运行时概念
When you execute type(b)
that's the instance of the
class int().
不完全是。
这里的int实例在调用int()
时开始存在1这个进程就是所谓的"instantiation" 并且结果(由此调用返回,在此示例中然后分配给 b
)是 int
.
的 "instance"
But then I read this stack overflow answer:
Instance is a variable that holds the memory address of the Object.
哦,这不太正确。 object 本身(如果您愿意,该内存地址处的值)就是实例。多个变量可能绑定到同一个 object(因此是同一个实例)。甚至还有一个用于测试的运算符:is
>>> a = 5
>>> b = a
>>> a is b
True
Which makes me a little be confused about the term. So when I assign a
variable at the moment of the execution the "instance" is created?
否,那么实例绑定到该变量。在 Python 中,将变量视为 "names for values"。因此,将 object 绑定到变量意味着为 object 赋予该名称。正如我们在上面看到的,object 可以有多个名称。
您可以使用实例而不将其分配给任何变量,即不命名它,例如通过将其传递给函数:
>>> print(int())
0
Finally this explanation in ComputerHope points to the fact that
instances are the same as variable assigments:
function Animal(numlegs, mysound) {
this.legs = numlegs;
this.sound = mysound;
}
var lion = new Animal(4, "roar");
var cat = new Animal(4, "meow");
var dog = new Animal(4, "bark");
The Animal object allows for the number of legs and the sound the
animal makes to be set by each instance of the object. In this case,
all three instances (lion, cat, and dog) have the same number of legs,
but make different sounds.
不幸的是,ComputerHope 上的解释可能会让大多数读者感到困惑,而不是帮助他们。首先,它合并了术语 "class" 和 "object"。他们的意思不一样。 class 是一种类型的 object 的模板。 Objects 和 object 类型的模板不是同一个概念,就像曲奇饼干和曲奇饼不是一回事一样。
当然,[为了理解]在Python、class中并没有特别的帮助是(特殊,但不是太special)objects(类型type
)和JavaScript中的那个,直到引入class
概念,习惯上使用普通objects作为模板对于其他 objects。 (后一种方法被称为“prototype based object 方向”或 "prototype based inheritance"。相比之下,大多数其他 object 方向的语言,包括 Python,使用 class-based object 方向 / class-based 继承。我不太确定带有 class
关键字的现代 ECMAScript 属于哪个类别。)
Could anyone actually provide a clear definition of instance?
就像我在上面写的那样:
class 的 实例 是 object class 作为其类型.
所以 "instance" 总是 "instance of" 的东西。这也回答了标题中问题的语言学观点
When should I call it “instance”?
当你想称它为"instance of"时,你应该称它为"instance"(通常是class)。
1我没有说出全部真相。试试这个:
>>> a = int()
>>> b = int()
>>> a is b
True
等等什么? int
的两次调用不应该各自返回新实例,因此是两个不同的实例吗?
大多数类型都会发生这种情况,但某些 built-in 类型不同,int
就是其中之一。 CPython 实现的制造者意识到小整数被大量使用。因此,他们让 CPython 一直创建新的,每次需要相同的值时,他们只需要 re-use 相同的整数(相同的 object / 实例)。因为 Python 整数是不可变的,这通常不会导致任何问题,并且在 computation-intensive 程序中节省了大量内存和 object-creation-time。
Python 标准允许实现进行此优化,但 AFAIK 不要求它们这样做。所以这应该被认为是一个实现细节,你的程序逻辑永远不应该依赖于它。 (不过,您的性能优化可能依赖于它。)
我一直在阅读有关 python 的 OOP 文章,特别是 this one。
那篇文章的作者有描述和代码示例:
The Python syntax to instantiate a class is the same of a function call
>>> b = int()
>>> type(b)
<type 'int'>
据此我推断 "instance" 在执行时存在,而不是之前。当您执行 type(b)
时,这是 class int().
但后来我读到了这个stack overflow answer:
Instance is a variable that holds the memory address of the Object.
这让我对这个词有点困惑。因此,当我在执行时分配一个变量时,会创建 "instance" 吗?
最后这个explanation in ComputerHope指出实例与变量赋值相同的事实:
function Animal(numlegs, mysound) {
this.legs = numlegs;
this.sound = mysound;
}
var lion = new Animal(4, "roar");
var cat = new Animal(4, "meow");
var dog = new Animal(4, "bark");
The Animal object allows for the number of legs and the sound the animal makes to be set by each instance of the object. In this case, all three instances (lion, cat, and dog) have the same number of legs, but make different sounds.
谁能真正提供一个实例何时退出的明确定义?
通常在 OOP 中
Classes and objects are the two main aspects of object oriented programming. A class creates a new type where objects are instances of the class.
正如解释的那样here。
因此每次创建对象时,它都称为实例。
Python 在这个概念上没有区别,但是事情与其他语言有点不同,例如 Java。
事实上 Python 一切 都是对象,甚至 class 本身也是对象。
以下是其工作原理的简要说明:
考虑这个片段:
>>> class Foo:
... pass
...
>>> type(Foo)
<type 'type'>
>>>
Class Foo
是 type
类型,它是 Python 中所有 classes
的元 class
(然而 'old' 和 'new' class 之间有区别,更多 here, here and here)。
Class type
是一个 class,是其自身的一个实例:
>>> isinstance(type, type)
True
所以 Foo
尽管是 class 定义,但被解释器当作对象对待。
作为实例的对象是通过像 foo = Foo()
这样的语句创建的,foo
作为对象继承自 object
class.
>>> isinstance(foo, object)
True
这个class提供了对象需要的所有方法,例如__new__()
和__int__()
(new, init)。简而言之,前者用于创建 class 的新实例,后者在创建实例后调用,用于初始化值,就像您对 Animal
.
一切皆对象这一事实也意味着我们可以编写如下有趣的代码:
>>> class Foo:
... var = 'hello'
...
>>> foo = Foo()
>>> foo.var
'hello'
>>> foo.other_var = 'world'
>>> foo.other_var
'world'
>>> Foo.other_var
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Foo' has no attribute 'other_var'
>>> Foo.var
'hello'
>>>
这里我在运行时在对象上添加了一个属性。该属性将在 foo
中唯一存在,class 本身或任何其他实例都不会拥有它。
这叫做Instance variable and class variable.
希望这一切对你有意义。
TL;DR
在Python中,一切(class定义、functions、模块等)都被解释器视为对象。因此 'everything' 是一个实例。
I've been reading articles about OOP with python, specifically this one.
The autor of that article has a description and then a code example:
The Python syntax to instantiate a class is the same of a function call
>>> b = int() >>> type(b) <type 'int'>
再读一遍前面的句子:
Once you have a class you can instantiate it to get a concrete object (an instance) of that type, i.e. an object built according to the structure of that class.
所以 class 的一个实例是一个 object,其类型是 class。
By this I infer "instance" exist at the moment of the execution and not before.
是的,正确。 "Instance" 和 "instance of" 是 Python.
中的运行时概念When you execute
type(b)
that's the instance of the class int().
不完全是。
这里的int实例在调用int()
时开始存在1这个进程就是所谓的"instantiation" 并且结果(由此调用返回,在此示例中然后分配给 b
)是 int
.
But then I read this stack overflow answer:
Instance is a variable that holds the memory address of the Object.
哦,这不太正确。 object 本身(如果您愿意,该内存地址处的值)就是实例。多个变量可能绑定到同一个 object(因此是同一个实例)。甚至还有一个用于测试的运算符:is
>>> a = 5
>>> b = a
>>> a is b
True
Which makes me a little be confused about the term. So when I assign a variable at the moment of the execution the "instance" is created?
否,那么实例绑定到该变量。在 Python 中,将变量视为 "names for values"。因此,将 object 绑定到变量意味着为 object 赋予该名称。正如我们在上面看到的,object 可以有多个名称。
您可以使用实例而不将其分配给任何变量,即不命名它,例如通过将其传递给函数:
>>> print(int())
0
Finally this explanation in ComputerHope points to the fact that instances are the same as variable assigments:
function Animal(numlegs, mysound) { this.legs = numlegs; this.sound = mysound; } var lion = new Animal(4, "roar"); var cat = new Animal(4, "meow"); var dog = new Animal(4, "bark");
The Animal object allows for the number of legs and the sound the animal makes to be set by each instance of the object. In this case, all three instances (lion, cat, and dog) have the same number of legs, but make different sounds.
不幸的是,ComputerHope 上的解释可能会让大多数读者感到困惑,而不是帮助他们。首先,它合并了术语 "class" 和 "object"。他们的意思不一样。 class 是一种类型的 object 的模板。 Objects 和 object 类型的模板不是同一个概念,就像曲奇饼干和曲奇饼不是一回事一样。
当然,[为了理解]在Python、class中并没有特别的帮助是(特殊,但不是太special)objects(类型type
)和JavaScript中的那个,直到引入class
概念,习惯上使用普通objects作为模板对于其他 objects。 (后一种方法被称为“prototype based object 方向”或 "prototype based inheritance"。相比之下,大多数其他 object 方向的语言,包括 Python,使用 class-based object 方向 / class-based 继承。我不太确定带有 class
关键字的现代 ECMAScript 属于哪个类别。)
Could anyone actually provide a clear definition of instance?
就像我在上面写的那样: class 的 实例 是 object class 作为其类型.
所以 "instance" 总是 "instance of" 的东西。这也回答了标题中问题的语言学观点
When should I call it “instance”?
当你想称它为"instance of"时,你应该称它为"instance"(通常是class)。
1我没有说出全部真相。试试这个:
>>> a = int()
>>> b = int()
>>> a is b
True
等等什么? int
的两次调用不应该各自返回新实例,因此是两个不同的实例吗?
大多数类型都会发生这种情况,但某些 built-in 类型不同,int
就是其中之一。 CPython 实现的制造者意识到小整数被大量使用。因此,他们让 CPython 一直创建新的,每次需要相同的值时,他们只需要 re-use 相同的整数(相同的 object / 实例)。因为 Python 整数是不可变的,这通常不会导致任何问题,并且在 computation-intensive 程序中节省了大量内存和 object-creation-time。
Python 标准允许实现进行此优化,但 AFAIK 不要求它们这样做。所以这应该被认为是一个实现细节,你的程序逻辑永远不应该依赖于它。 (不过,您的性能优化可能依赖于它。)