4

In x = 1, are both x and 1 objects? Because print(1) and x = 1; print(x) will result in the same output.

Even the syntax of print function is:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

  • 5
    `x` is a *name* which holds the *value* `1`, which is an object. – deceze Jun 17 '20 at 15:59
  • 5
    Even `1` is not an object; it's a *literal* that evaluates to an object. – chepner Jun 17 '20 at 16:02
  • 3
    Be sure to read https://nedbatchelder.com/text/names.html – chepner Jun 17 '20 at 16:03
  • ``x`` is a name. The only reason ``print(x)`` results in ``1`` is because names are *not* objects, but resolve to the objects they point to. – MisterMiyagi Jun 17 '20 at 16:05
  • Related questions: [If two variables point to the same object, why doesn't reassigning one variable affect the other?](https://stackoverflow.com/questions/56667280/if-two-variables-point-to-the-same-object-why-doesnt-reassigning-one-variable) and [Are python variables pointers? or else what are they?](https://stackoverflow.com/questions/13530998/are-python-variables-pointers-or-else-what-are-they) – MisterMiyagi Jun 17 '20 at 16:06

2 Answers2

4

Names in Python are not objects. Using a name in an expression automatically evaluates to the object referred to by the name. It is not possible to interact with the name itself in any way, such as passing it around or calling a method on it.

>>> x = 1
>>> type(1)    # pass number to function...
<class 'int'>  # ...and receive the number!
>>> type(x)    # pass name to function...
<class 'int'>  # ...but receive the target!

Note that technically, 1 is also not an object but a literal of an object. Only the object can be passed around – it does not reveal whether it originates from a literal 1 or, for example, a mathematical expression such as 2 - 1.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • But when the program is run, ```1``` becomes an ```int``` object in memory, right? –  Jun 17 '20 at 16:15
  • 1
    When the expression ``1`` is evaluated, its result is an ``int`` object of value 1. – MisterMiyagi Jun 17 '20 at 16:16
  • Hey, what does this mean? - *"Only the object can be passed around – it does not reveal whether it originates from a literal 1 or, for example, a mathematical expression such as 2 - 1."* –  Jun 17 '20 at 16:34
  • 1
    It means that just like a function cannot know whether the argument value 1 came from the literal ``1`` or name ``x``, it also cannot know whether the value 1 came from the literal ``1`` or the literal expression ``2-1``. ``1`` just happens to be the literal *and* standard representation of the value 1, but it is not this value. – MisterMiyagi Jun 17 '20 at 16:39
  • *"1 just happens to be the literal and standard representation of the value 1, but it is not this value"* - I didn't understand this point. –  Jun 17 '20 at 18:10
  • 1
    @NSR It is customary to use the expression consisting of the standard representation of an object as a stand-in for this object. For example, the literal expression ``1`` to refer to the integer of value 1, or the literal expression ``True`` to refer to the boolean of value True. *This imprecision works totally fine in day to day use*. However, when looking closely at how the language works, this can be confusing to think the literal expression (`1`) gets passed to functions even though the name expression (`x`) does not – when in fact in both cases only the object gets passed on. – MisterMiyagi Jun 18 '20 at 09:02
  • If I got it correctly, then you are implying that if there is a function call ```f(1, 2)```, then when this is evaluated, ```1``` and ```2``` are not literals now but they have become **objects** that are passed to the function. Right? –  Jun 18 '20 at 10:14
1

1 is an int object. x is a variable which has a reference to the object.

For more in depth on pass-by-reference vs pass-by-value see this answer. It says:

The variable is not the object.

print() will output the representation of the object, which 1 and x both point to.

What is interesting in this case is that you could create multiple instances of identical objects by simply creating more variables that have the same value, but point to different instances. For example:

x = 1000
y = 1000
z = 1000

These are 3 different objects which are equal to each other, but still separate objects.

For numbers from -5 to 255, the python interpreter will cache the object instances so that all Integers in that range only have one instance. If the above example were 1 instead of 1000, x,y, and z would actually point to the same object.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Jon
  • 1,820
  • 2
  • 19
  • 43