5

It was written here that Python has both atomic and reference object types. Atomic objects are: int, long, complex. When assigning atomic object, it's value is copied, when assigning reference object it's reference is copied.

My question is: why then, when i do the code bellow i get 'True'?

a = 1234
b = a
print id(a) == id(b)

It seems to me that i don't copy value, i just copy reference, no matter what type it is.

Umair M
  • 10,298
  • 6
  • 42
  • 74
Artem Sliusar
  • 61
  • 1
  • 5
  • 7
    That page is wrong then. – vaultah Aug 24 '16 at 14:03
  • 6
    In Python simple assignment (`name = something`) never creates copies. See [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html) by [Ned Batchelder](http://stackoverflow.com/users/14343/ned-batchelder). – Steven Rumbalski Aug 24 '16 at 14:05
  • 2
    Python only has references. More interesting is whether they refer to mutable or immutable objects. – spectras Aug 24 '16 at 14:08
  • 1
    Since the value *is* atomic and *immutable*, why would Python need to create a copy of this simple number? `1234` is `1234`, it only needs to be stored in memory once. – deceze Aug 24 '16 at 14:14

4 Answers4

7

Assignment (binding) in Python NEVER copies data. It ALWAYS copies a reference to the value being bound.

The interpreter computes the value on the right-hand side, and the left-hand side is bound to the new value by referencing it. If expression on the right-hand side is an existing value (in other words, if no operators are required to compute its value) then the left-hand side will be a reference to the same object.

After

a = b

is executed,

a is b

will ALWAYS be true - that's how assignment works in Python. It's also true for containers, so x[i].some_attribute = y will make x[i].some_attribute is y true.

The assertion that Python has atomic types and reference types seems unhelpful to me, if not just plain untrue. I'd say it has atomic types and container types. Containers are things like lists, tuples, dicts, and instances with private attributes (to a first approximation).

As @BallPointPen helpfully pointed out in their comment, mutable values can be altered without needing to re-bind the reference. Since immutable values cannot be altered, references must be re-bound in order to refer to a different value.

Edit: Recently reading the English version of the quoted page (I'm afraid I don't understand Russian) I see "Python uses dynamic typing, and a combination of reference counting and a cycle-detecting garbage collector for memory management." It's possible the Russian page has mistranslated this to give a false impression of the language, or that it was misunderstood by the OP. But Python doesn't have "reference types" except in the most particular sense for weakrefs and similar constructs.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • 2
    The reason it "feels" like Python differentiates between value types and reference types is that some types are immutable, which means the only way to change the value of a variable of that type is to reassign, pulling the reference off of the old value. You can even check that `a = 1; print(id(a)); a += 1; print(id(a))` will produce two different values of `id`. Meanwhile mutable types can be modified in place, which makes them feel more reference-y. The real distinction lies in (im)mutability. – BallpointBen Apr 13 '18 at 15:28
0

int types are immutable. what you see is the reference for the number 1234 and that will never change.

for mutable object like list, dictionary you can use

import copy
a = copy.deepcopy(b)
nivhanin
  • 1,688
  • 3
  • 19
  • 31
0

If standard Python data types cannot garantee the atomicity of your data operations , you could take a look at the shared atomic enterprise python data types at https://sharedatomic.top

-1

Actually like @spectras said there are only references but there are immutable objects like floats, ints, tuples. For immutable objects (apart from memory consumption) it just does not matter if you pass around references or create copies.

The interpreter even does some optimizations making use of numbers with the same value being interchangeable making checking numbers for identity interesting because eg for

a=1
b=1
c=2/2
d=12345 
e=12345*1

a is b is true and a is c is also true but d is e is false (== works normally as expected)

Immutable objects are atomic the way that changing them is threadsafe because you do not actually change the object itself but just put a new reference in a variable (which is threadsafe).

janbrohl
  • 2,626
  • 1
  • 17
  • 15