0

If I defined a class

class myclass:

    def __init__(self, variable):    
        a = variable * 100
        self.a = a*1000

and I created an instance of the myclass object

myinstance = myclass([1])

After initialization, does python still keep a in it's memory? or does it only store self.a?

martineau
  • 119,623
  • 25
  • 170
  • 301
Tian
  • 870
  • 3
  • 12
  • 24
  • 1
    No, `a` is a local variable, and it ceases to exist after the function terminates – juanpa.arrivillaga Oct 02 '19 at 07:03
  • 1
    In addition to what @juanpa.arrivillaga said: the variable (or rather the object, that your variable name is pointing to) will be flagged for garbage collection, since no other variable names are referencing it once the function terminates. So it may be kept in memory for a little, until the garbage collector decides to free the memory. – Mike Scotty Oct 02 '19 at 07:05
  • @MikeScotty indeed, in CPython objects are reclaimed *immediately* when their reference count reaches 0, but that is an implementation detail – juanpa.arrivillaga Oct 02 '19 at 07:07

0 Answers0