Why only one instance is created for every object i create and is there any other way to create multiple instances | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why only one instance is created for every object i create and is there any other way to create multiple instances

https://code.sololearn.com/cRITQAbE09r6/?ref=app

7th Jul 2020, 1:04 PM
ashu pandey
ashu pandey - avatar
3 Answers
+ 5
Granger is absolutely right. class variables are shared in all instances. But there are 3 different instances created, you can test this with print(id(a),id(b),id(c)) in the line after the instances have been created. It shows 3 different object id's.
7th Jul 2020, 3:44 PM
Lothar
Lothar - avatar
+ 5
This is something different than the item you aksed for. in this case: a=10 b=a print(id(a), id(b)) # e.g. 16146984 16146984 - both use the same id a=7 print(id(a), id(b)) # e.g. 16146936 16146984 - now a has a new id and is different from b. b uses a reference to a, so it shows the same value and the same id. But if you later assign an other value to a (7), a gets a new id, so there is no longer a "retionship" between them.
7th Jul 2020, 4:47 PM
Lothar
Lothar - avatar
0
Then help me with a = 10 b = a Here both have same id or same reference then i know only one way by adding 0 if varaiable is type int and '' empty string if type str is there any other way thanks anyway
7th Jul 2020, 4:34 PM
ashu pandey
ashu pandey - avatar