简体   繁体   English

Python中的实例和object有什么区别?

[英]What is the difference between an instance and an object in Python?

I know that this question has been asked and answered many times but the answers to those questions aren't really assuaging.我知道这个问题已经被问过很多次了,但这些问题的答案并没有真正让人放心。 Each person has given a different definition of both which is really confusing.每个人对两者都有不同的定义,这确实令人困惑。 Being a novice, I really don't know whom to trust.作为一个新手,我真的不知道该相信谁。 Can someone please clarify the difference between them?有人可以澄清它们之间的区别吗?

In Python, everything is an object.在 Python 中,一切都是 object。 Moreover in Python objects are instances of classes, so it follows that every object is also an instance of some class * .此外,在 Python 中,对象是类的实例,因此每个 object 也是一些 class *的实例。

However, we generally use the term instance in preference to object when we want to discuss the behaviour of instances of a specific class or classes但是,当我们想讨论特定class或类的实例的行为时,我们通常优先使用术语实例而不是 object

Instances of Foo provide the following operations... Foo的实例提供以下操作...

No two instances of Bar may compare as equal...没有两个Bar实例可以比较相等...

So we might say that object is the most general term, whereas instances refers to the set of objects which are instances of a particular class or classes, and instance to a specific object which is an instance of a particular class. So we might say that object is the most general term, whereas instances refers to the set of objects which are instances of a particular class or classes, and instance to a specific object which is an instance of a particular class.

In short, they are the same thing, but we use these terms in different contexts.简而言之,它们是相同的东西,但我们在不同的上下文中使用这些术语。

* Python enables this circular definition by making object an instance of type , and type an instance of object , and both object and type are instances of themselves. * Python enables this circular definition by making object an instance of type , and type an instance of object , and both object and type are instances of themselves.

instance and object are effectively the same thing. instanceobject实际上是同一件事。 When you create an instance of a class, that instance is an object.当您创建 class 的实例时,该实例是 object。

Put another way, every instance is an object.换句话说,每个实例都是一个 object。 That is the literal definition of an object: an instance of a class.这是 object 的字面定义:class 的实例

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM