Python create new object with the same value?

Assuming integers, I cannot reproduce your issue.

Assuming integers, I cannot reproduce your issue: >>> a = 1 >>> be = a >>> a += 1 >>> a 2 >>> be 1 If we assume objects instead: class Test(object): ... def __init__(self, v): ... self. V = v ... >>> a = Test(1) >>> be = a. V >>> a.

V += 1 >>> print a. V, be 2 1 # No issues so far # Let's copy the object instead >>> be = a >>> a. V += 1 >>> print a.

V, b. V 3 3 # Ah, there we go # Using user252462's suggestion >>> from copy import deepcopy >>> be = deepcopy(a) >>> a. V += 1 >>> print a.

V, b. V 4 3.

Note that this is exactly how it works in Java and most other languages. Nothing unusual here. – delnan May 5 '11 at 19:50 @delnan: In C++, be = a creates copies of user defined classes by default (unless you overload the assignment operator).

– Sven Marnach May 5 '11 at 19:57 @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. – delnan May 5 '11 at 20:03.

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.

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.

– delnan May 5 '11 at 20:05.

This documentation might help out: docs.python.org/library/copy.html You can use the copy library to deepcopy objects: import copy be = copy. Deepcopy(a).

I'm not sure what you're seeing here. >>> a = 1 >>> be = a >>> a = a + 1 >>> be 1 >>> a 2 >>> a is be False Python Integers are immutable, the + operation assigns creates a new object with value a+1. There are some weird reference issues with integers (distilledb.com/blog/archives/date/2009/0...), but you should get the same thing you expected in Java.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions