What does this mean "In Python, all literal values result in the creation of objects"?
I am learning Python, and got this sentence from here: http://python4java.necaiseweb.org/Fundamentals/FunctionsAndMethods
But I does not really understand its meaning.
First, what are "literal values"?
Second, please explain this sentence.
If you could make some examples, that would be helpful!
Python actually has a few definitions for "literal" in different contexts, but if you just want the basic idea: Numbers like 123 and strings like "abc" are literals; expressions like 123 + 456 are not.
In Java, when you write 123, that's not a reference to a Java object, it's a "native" integer. In Python, it is a reference to a Python object.
Because everything is an object in Python, an integer has methods, can be stuck in collections, etc. There's no "boxing" and "unboxing" anywhere. If I want to stick 123 into a list, I just do it:
>>> my_list = list()
>>> my_list.append(123)
>>> my_list
[123]
And if I want to use the value from the list as an integer, I just do it:
>>> my_list[0] - 120
3
For that matter, I can just write a list display, using literals just like other objects:
>>> my_other_list = [my_list, 2]
(Just don't ask whether a list display is also a literal, because that's when the "different definitions for different contexts" actually matters…)
It's worth pointing out that it's not really true that "all literal values result in the creation of objects". A literal may be a new object, but it may also be a reference to existing object with the same value that. For example:
>>> a = 3
>>> b = 3
>>> a is b
True
>>> id(a) == id(b)
True
(This isn't guaranteed to be true by the language, but it usually will be on most Python implementations.)
So, b = 3 did not result in the creation of an object, just another reference to the same object in a = 3. (And in fact, that 3 was most likely already pre-built and pre-cached by the interpreter before even looking at your code.)
But you don't need to care about this, because 3 is immutable. It doesn't matter if you get the same 3 object or a different one because, short of is and id, there is no way to tell the difference. And the same is true for strings, floats, etc.
The following are examples of literal values
>>> 0
0
>>> 'a'
'a'
>>> 1.9
1.9
>>> 'foo'
'foo'
Everything is an object, even ints, as shown below you can see the methods of 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']
If I want I can call:
>>> (-1).__abs__() # abs(-1) is better but this is just to show it's an object
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With