On Thu, Jan 10, 2013 at 3:06 PM, richard kappler <richkapp...@gmail.com>wrote:
> Class is still something I struggle with. I think I'm finally starting to > get my head wrapped around it, but the discussion in a different thread has > sparked a question. First, please check my understanding: > A class creates objects, it's like a template that allows me to create as > many copies as I want of the object but allows me to have slightly > different versions of the object by having different values for the > variables within the object, which I can set with arguments? > By using __init__ (self) I instantiate a new copy of the object? > > This is essentially correct. There are some intricacies in python with __init__, which doesn't create a new object but merely initiates it. Python also has __new__, which actually creates a new instance. But this is mostly just details, in 99% of cases we can simply write an __init__ method for initialization and not worry about __new__ > Whether the above is correct or not (and do please correct me/ tutor me), > my question is, if I create objects in a while True loop, do the objects > get garbage collected, ie. disappear when the loop returns to the beginning > and creates new versions of the objects? > > Psuedo code example: > > (presuming I have a class that creates a lemon, a lime and has a function > that operates on them called juice) > > while True: > lemon = yellow() # create a yellow object called lemon > lime = green() # create a green object called lime > drink = juice(lemon, lime) # operate on objects lemon and lime in a > function called juice resident in the class > > So, using the above example, when the loop reaches the end and returns to > the beginning, new lemons and limes are created, yes? What happens to the > old ones? Or have I still got this completed boggled? > > In a general sense, objects are garbage collected when there are no more references to these objects, i.e. when you become unable to use them. On each pass of the loop, you make the names lemon, lime and drink point to new instances. The instances they were pointing to previously thus become inaccessible. If there are no other references to these objects in the program, they will be garbage collected (whether this happens immediately or after a while differs depending on python implementation). Hugo
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor