The main (and probably sufficient) answer:
The variable foo assigned to a new instance that knows nothing about the first. It will basically be the same as:
a = 1 a = 2
At some point (probably sooner than later) the old foo instance will be garbage collected (if you don't already have references to it somewhere else).
An easy way to figure this out is that the name foo is just a pointer pointing to the actual object. If you have a foo point on a new object, then foo points to a new object, period.
Matching Detail: Singleton Template
There are design patterns ( singleton that come to mind) that can change this behavior.
Basically, the class method of Foo.__new__ is the one that decides what you are going to do when you do Foo() .
This method can be built in such a way that it always returns the first instance that you created, but this spectacle is off topic.
Here's an example implementation:
class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) return cls._instance
source share