如何描述Python对象,以及属性、标识、类型和值之间的关系?

2024-04-20 05:42:39 发布

您现在位置:Python中文网/ 问答频道 /正文

(关于Python 3.2)

我想说的是:

In Python, an object is...

根据文件(http://docs.python.org/py3k/reference/datamodel.html#objects-values-and-types):

Every object has an identity, a type and a value

但是,属性在哪里呢?如果我做像a = 3; print(a.__class__)这样的事情,我得到<class 'int'>我假设这是对象a引用的类型,这意味着“类型”是对象的“属性”。因此在这个意义上,我们可以说一个对象有足够的“事物”集合,即它的身份、价值和属性。然而,纵观usingdir(a)的属性,我看不到任何类似于identity的东西(即使我知道id()函数会告诉我这些信息)。

所以我的问题是,下面的任何一个最小语句是否足以描述Python对象的概念?

  1. In Python an object has attributes, of which always include an identity, type and value.

  2. In Python an object has an identity and attributes, of which always include its type and value.

  3. In Python an object has an identity, value and attributes, of which always include its type, among other things.

如果不是,有人能给我一个定义,传达一个对象的关系属性,身份,类型和值吗?

(我更希望数字1是真的。:P页)


Tags: andof对象inan类型which属性
3条回答

我想你把术语弄混了。

身份

对象的标识只是一个值,它在对象的生命周期中唯一标识该对象。因此,id(1)保证与id(2)不同。

类型

对象的类型告诉您更多信息;它告诉您可以对该对象使用哪些操作,以及可以在该对象中存储哪些可能的值。有两种类型:内置类型和用户定义类型。内置类型是intstring等。用户定义的是您自己定义的类。

价值

这是存储在类型中的内容。

属性

这些是可以从现有对象获取的其他变量。

示例:

>>> s = "foo" # s is a new variable
>>> id(s) # it has a unique identifier
4299579536
>>> type(s) # it has a type ("str")
<type 'str'>
>>> s # it has a value ("foo")
'foo'
>>> s.__class__ # it has an attribute called __class__ that has the following value:
<type 'str'>

所以你的三个陈述都是正确的,但对我来说这三个听起来“最正确”。我也听说过

In Python, an object is a dict.

这对我来说是有意义的,但对每个人来说可能都没有意义。

虽然可以通过属性访问对象的类型,但它的类型不仅仅是一个属性——该类型定义了对象在拥有任何属性之前是如何创建的。仅凭这个事实,这些语句都不足以描述Python对象。

我这么说:

In Python, everything is an object.

An object is a block of information, which has a type, which defines its creation and how it interacts with other objects, an identity, which differentiates it from all other objects, and a value, which is the information in the block. Attributes are other objects associated with a given object, including the object that is its type.

然后,您应该给出一些人们可能不希望成为对象的例子,比如函数。

关于“什么是对象”的段落可以在Dive Into Python中找到:

Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function's source code. The sys module is an object which has (among other things) an attribute called path. And so forth.

Still, this begs the question. What is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4).

But where do attributes fall into that?

(某些组合)特定对象的属性记录并确定其值。

价值、类型和身份这三个概念都是抽象的,只有通过例子才能真正理解。考虑:

>>> a = (1, 2)
>>> b = (1, 2)
>>> c = a
>>> d = (3, 4)
>>> e = 2

毫无疑问,您知道abc都是相等的,但只有a和c是同一个对象。也就是说,这三个值都是相同的,而a和c具有相同的身份。da(元组)是同一类型的对象,但具有不同的值;e是完全不同的类型(int)。 对于人类来说,e的值很容易理解(因为int字面值是用众所周知的符号拼写的),但是Python会跟踪它的几个属性:

>>> e.numerator
2
>>> e.denominator
1
>>> e.real
2
>>> e.imag
0

它们一起决定值-虽然是int,但可以保证分母是1,虚部是0(实际上,所有内置的Rationals都有一个零虚部)。因此,int的值是它的分子。

但“价值”是一种抽象的东西——它对人类来说是有意义的,计算机必须把它再细分一点。类似于类型-“这是什么类型的东西?”是人类直接理解的东西;计算机不理解,但Python使用类来实现它——因此

>>> type('')
<class 'str'>
>>> type(2)
<class 'int'>

你问“这是什么东西?”,它还给你它的类。类型存储为属性,但这只是因为Python认为对象的类型是其值的(潜在的)一部分。标识不是值的一部分(尽管在某些情况下,它们是等价的),因此它不会记录在属性中—它存储在内部机器中,当Python说a时,该机器允许Python找到所需的对象。

相关问题 更多 >