[Tutor] garbage collection/class question
Mitya Sirenef
msirenef at lightbird.net
Thu Jan 10 18:50:01 CET 2013
On 01/10/2013 09:06 AM, richard kappler 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?
There are good answers already, I just want to address this question;
you are correct but classes allow you to do other things, too. You may
want to use a class even if you don't need multiple instances. Classes
allow you to group related functionality and data together:
class Tree(object):
height = 0
def grow(self):
self.height += 1
You may have a dozen of related functions and you can logically group
them together by making them methods of a class, making it easier to
think about and work on the logic of your program.
Classes also create a namespace for each instance:
x = 1
class A(object):
x = 2
a = A()
a.x = 3
Here, a.x is completely independent of the global namespace's 'x'. In
instance methods, it can be called with 'self.x' .
There are other things classes provide, but these are the most important
in small / medium size programs. You can read up on OOP here:
http://en.wikipedia.org/wiki/Object-oriented_programming
HTH, - m
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
More information about the Tutor
mailing list