[Tutor] garbage collection/class question

Dave Angel d at davea.name
Thu Jan 10 16:31:44 CET 2013


On 01/10/2013 09:06 AM, richard kappler wrote:

Since you don't specify Python version or implementation, I'll use
CPython version 2.7 for the details below.  Jython (for java
environments) and other implementations are likely to differ in their
garbage collection details.  But the effect will be the same.  I'm also
assuming new-style classes, which are all that's available in Python 3,
but in 2.7, you get one by making sure your class is derived from object.

> 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?

Python is already full of class objects.  The only special thing about
'class' is it lets you define your own types of objects.  There are
dozens of built-in and library types, and you're probably already
familiar with many of them.  For example, every integer is an instance
of the int class.  Python has a special syntax that lets you create them
invisibly, but they are there, nonetheless.  I don't think it's useful
to think of them as copies of a master int, but rather as independent
int objects, each with some content that may or may not be unique.

> By using  __init__ (self) I instantiate a new copy of the object?

The new INSTANCE is made when somebody invokes  MyClass(arg1).  During
that creation, two methods of MyClass are called.  The __new__() method
actually creates the empty class object, and associates methods and such
to it.  And then the __init__() method gets control and can add other
attributes to it.  Most programs let the default __new__() method do its
thing.


> 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)

But your code below does not fit the sentence above.

> while True:
>     lemon = yellow() # create a yellow object called lemon

Actually, the object has no name.  The name lemon is bound to the object
in that line, but it's really a one-way binding.  However, CPython keeps
a count of such bindings for each object, called a reference count.

>     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

Since you don't give any context, I don't know where() juice is defined,
or where this loop is defined, nor what you mean by 'the class'.  But if
you mean what you say, then you're missing an object parameter to the
juice method.

> So, using the above example, when the loop reaches the end and returns to
> the beginning, new lemons and limes are created, yes?

Not exactly.  New instances of class yellow and class green are created,
and the names lemon and lime are unbound from the old objects, and bound
to those new objects.  If this loop is all the code, then the old
objects now have zero reference counts and will be immediately freed. 
Technically this is different than garbage collection, but the effect is
the same.

>  What happens to the
> old ones? Or have I still got this completed boggled?
>
> regards, RIchard
>
>

Objects that have no bindings are inaccessible, and eventually go away
(get freed).



-- 

DaveA



More information about the Tutor mailing list