+ 1

A pythonic question!

Class Car: pass x = Car() print(x) print(Car()) Output is: <__main__.Car object at 0x2b5727b0bef0> <__main__.Car object at 0x2b5727b0be80> Why am I getting different memory locations? Shouldn't they be the same? Calling a method in both mentioned ways works properly and identically, so what makes any diffent in using the class name directly or assigning it to a variable? Aren't they both instantiation?

2nd Oct 2020, 4:43 PM
Michael
3 Respuestas
+ 3
When you create the car class it gets a place in the memory after instantiating x object the object receives different location cuz it's not a copy it is different object
2nd Oct 2020, 4:53 PM
Hamza_01
Hamza_01 - avatar
+ 4
No, each time you call Car() you instantiate a new Car object, different then the last. It will be a different address as well
2nd Oct 2020, 4:46 PM
Slick
Slick - avatar
+ 2
It is a bit like A=next(iterator) print(next(iterator)) Bad pitfall
2nd Oct 2020, 5:20 PM
Oma Falk
Oma Falk - avatar