Python create new object with the same value [duplicate]
Asked Answered
L

5

5

I come from java world where I expect following things

int a = valueassignedbyfunction();
int b = a;
a = a + 1; 

after this a is 1 greater than b. But in python the b automatically gets incremented by one once the a = a + 1 operation is done because this b is referencing to the same object as a does. How can I copy only the value of a and assign it to a new object called b?

Thanks!

Legalese answered 5/5, 2011 at 19:15 Comment(1)
I think you are wrong here. Integer objects are immutable in Python. They can't be changed.Albuminoid
D
15

Assuming integers, I cannot reproduce your issue:

>>> a = 1
>>> b = a
>>> a += 1
>>> a
2
>>> b
1

If we assume objects instead:

class Test(object):
...     def __init__(self, v):
...         self.v = v
...         
>>> a = Test(1)
>>> b = a.v
>>> a.v += 1
>>> print a.v, b
2 1
# No issues so far
# Let's copy the object instead
>>> b = a
>>> a.v += 1
>>> print a.v, b.v
3 3
# Ah, there we go
# Using user252462's suggestion
>>> from copy import deepcopy
>>> b = deepcopy(a)
>>> a.v += 1
>>> print a.v, b.v
4 3
Decennial answered 5/5, 2011 at 19:24 Comment(3)
Note that this is exactly how it works in Java and most other languages. Nothing unusual here.Wellspoken
@delnan: In C++, b = a creates copies of user defined classes by default (unless you overload the assignment operator).Albuminoid
@Sven: Yes, but C++ is rather unique among widely used high-level languages (in several areas, not only here). For example, Java, C#, VB.NET, Ruby, and propably many others have similar semantics. Admittedly, should be s/most/many/ in my first comment, but still applies for OP.Wellspoken
C
5

This documentation might help out: http://docs.python.org/library/copy.html

You can use the copy library to deepcopy objects:

import copy
b = copy.deepcopy(a)
Catherin answered 5/5, 2011 at 19:20 Comment(1)
I think this is wrong, per the answers from Sean, Sven and dfb, integers in Python are immutable, you'd only use copy on mutable objects.Caret
A
5

I think the main confusion here is the following: In Java, a line like

int i = 5;

allocates memory for an integer and associates the name i with this memory location. You can somehow identify the name i with this memory location and its type and call the whole thing "the integer variable i".

In Python, the line

i = 5

evaluates the expression on the right hand side, which will yield a Python object (in this case, the expression is really simple and will yield the integer object 5). The assignment statement makes the name i point to that object, but the relation between the name and the object is a completely different one than in Java. Names are always just references to objects, and there may be many names referencing the same object or no name at all.

Albuminoid answered 5/5, 2011 at 19:54 Comment(1)
Note that for Objects, Java (as well as several other languages) too only store a reference with all of the observable effects. It's only the special casing for primitive types that differs so radically. Still +1'd as it provides a decent writeup on the variables and object model.Wellspoken
E
3

I'm not sure what you're seeing here.

>>> a = 1 
>>> b = a
>>> a = a + 1
>>> b
1
>>> a
2
>>> a is b
False

Python Integers are immutable, the + operation assigns creates a new object with value a+1. There are some weird reference issues with integers (http://distilledb.com/blog/archives/date/2009/06/18/python-gotcha-integer-equality.page), but you should get the same thing you expected in Java

Ebert answered 5/5, 2011 at 19:20 Comment(0)
T
0

How about just doing

a = 1
b = a*1
Traction answered 19/9, 2020 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.