2

这是什么意思“在 Python 中,所有文字值都会导致对象的创建”?

我正在学习 Python,并从这里得到这句话:http: //python4java.necaiseweb.org/Fundamentals/FunctionsAndMethods

但我并不真正理解它的含义。

首先,什么是“字面价值”?

其次,请解释一下这句话。

如果你能举一些例子,那会很有帮助!

4

4 回答 4

3

Python 实际上在不同的上下文中对“文字”有一些定义,但如果您只想要基本概念: 数字123和字符串之类"abc"是文字;像123 + 456不是这样的表达。

在 Java 中,当您编写 时123,这不是对 Java 对象的引用,它是一个“本机”整数。在 Python 中,它对 Python 对象的引用。

因为在 Python 中一切都是对象,整数有方法,可以卡在集合中,等等。任何地方都没有“装箱”和“拆箱”。如果我想坚持123一个列表,我就这样做:

>>> my_list = list()
>>> my_list.append(123)
>>> my_list
[123]

如果我想将列表中的值用作整数,我就这样做:

>>> my_list[0] - 120
3

就此而言,我可以像其他对象一样使用文字来编写一个列表显示:

>>> my_other_list = [my_list, 2]

(只是不要问列表显示是否也是文字,因为那是“不同上下文的不同定义”真正重要的时候......)


值得指出的是,“所有文字值都会导致对象的创建”并不是真的。文字可能是一个新对象,但它也可能是对现有对象的引用,具有相同的值。例如:

>>> a = 3
>>> b = 3
>>> a is b
True
>>> id(a) == id(b)
True

(语言不保证这是真的,但它通常会出现在大多数 Python 实现中。)

因此,b = 3并没有导致对象的创建,只是对a = 3. (事实上​​,这3很可能在查看您的代码之前就已经由解释器预先构建和预先缓存了。)

但是你不需要关心这个,因为3它是不可变的。如果你得到相同的对象或不同的对象并不重要,3因为缺少isand id,没有办法区分。字符串、浮点数等也是如此。

于 2013-04-12T22:52:59.813 回答
2

以下是文字值的示例

>>> 0
0
>>> 'a'
'a'
>>> 1.9
1.9
>>> 'foo'
'foo'

一切都是对象,甚至是ints,如下图可以看到的方法0

>>> dir(0)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

如果我愿意,我可以致电:

>>> (-1).__abs__() # abs(-1) is better but this is just to show it's an object
1
于 2013-04-12T22:53:24.403 回答
2

这意味着当您键入任何值(如123467.2"this is a string")时,它将作为对象处理。

于 2013-04-12T22:53:41.850 回答
2

学习 Python 的一个非常好的方法是使用交互式解释器(通常通过Python在 shell 中键入来找到。)

完成此操作后,使用_typedirhelp来查看对象类型和信息,如下所示:

>>> 4             # a literal 4
4
>>> type(_)       # what is the type of the last object?
<type 'int'>      # int
>>> dir(int)      # what methods does it have?
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
>>> help(int)
... bunch of help text on int class ...

>>> type(4.4)
<type 'float'>
# do the same interview of Python to tell you about the float class...
于 2013-04-12T23:01:48.470 回答