What happens if I relate a second instance of a class with the same variable containing an existing instance?

I am not sure how to ask a question other than an example.

Let's say I have a class:

class Foo(): pass 

and I create an instance of it:

 foo = Foo() ... 

then, as part of my program, I recreate the same instance with the same name (restart the game, but keep the final grade in another, separate class called "Bar").

 foo = Foo() ... 

(I do not know the correct terminology). I have not written any code to eliminate the first instance. Is the first instance deleted or overwritten? This seems to work for a small program. If I do this many times, will I run into trouble?

+4
source share
7 answers

Let me explain this graphically.

When you do

 foo = Foo() 

the first time you create an object of type Foo in memory (with Foo() ) and at the same time create a variable that points to this object:

enter image description here

By doing

 foo = Foo() 

the second time you create another object. Meanwhile, you take the same variable and point it to a new object. Of course, the variable will no longer point to the previous object:

enter image description here

Now what happens to the first object? Well, if there is no other variable in your program pointing to it, it is useless, since you can no longer access it! In this case, the object will be destroyed / freed from memory when the interpreter starts the garbage collector. The garbage collector is an algorithm that will look for every object that is inaccessible and delete it. The garbage collector does not start every time, so it may take time to destroy the object.

So, in your scenario, the following happens:

  • The reference to the first object is overwritten, but the object will exist. If another variable is specified, it will not be destroyed.
  • However, if there is no other variable pointing to it, the object will be deleted at some point in the future.
+13
source

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 
+5
source

1st foo = Foo() :

it creates a new instance of the object and assigns it to foo , see foo - this is just a reference to the object returned by Foo()

2nd foo = Foo()

it creates a new instance of the object and assigns it to foo , and the previous instance object of Foo() is garbage collected since there is no variable pointing to it.

Example:

 >>> class Foo: ... pass ... >>> >>> foo=Foo() >>> id(foo) #foo points to an object with id 3077166220L 3077166220L >>> foo=Foo() #now foo points to some other object and the object with id 3077166220L is garbage collected >>> id(foo) 3077166252L >>> >>> >>> temp=foo #now both foo and temp point to same object ie 3077166252L >>> foo=Foo() #assign foo to a new object >>> id(temp) #temp still points 3077166252L ,so it not garbage collected 3077166252L >>> id(foo) #it points to a new object 3077166092L 
+2
source

A few comments.

  • The name "foo" is a variable defined in your code. In Python, a variable has no type until you assign a value to it.
  • When you say foo = Foo() , you assign the variable foo instance of an object of type foo . Foo() creates this instance by calling the constructor of the foo class without any parameters.
  • Each call to Foo() creates a new instance. Therefore, repeating foo = Foo() in your code will create several instances of the class foo and assign them to the variable foo . Previously, instances pointed to by the variable foo no longer have references and can be garbage collected (assuming you did not assign other references to these instances at the same time).

Therefore, the words you use to describe your question

create a second instance of the class with the same name as the existing instance

not really right. Because you are not creating another instance with the same name. You create another instance and assign it to the "same variable".

+1
source
  • There is no rewriting in Python.
  • When you write Foo() , a new instance is created (the class method __new__ and the instance method __init__ are called in order)
  • When there is no pointer to an instance of the class, the Python Garbage Collector will eventually delete that instance (and the __del__ method __del__ is called on that instance)
0
source

As a result, you will attach the new Foo object to foo , and your old foo object will lose the link. If the object has no references, it will be collected in the next garbage collection.

0
source

The second will cancel the first. (It replaces the name in the global dictionary of the module, where all global variables, functions and classes are stored).

However, the foo = Foo () you created will continue to use the original implementation from the moment it is created until you create a new foo = Foo ().

0
source

All Articles