[Tutor] garbage collection/class question

Mitya Sirenef msirenef at lightbird.net
Sat Jan 12 01:24:54 CET 2013


On 01/11/2013 04:32 PM, Dave Angel wrote:
> On 01/11/2013 02:41 PM, Jan  Riechers wrote:
 >> On 10.01.2013 19:50, Mitya Sirenef wrote:
 >>> On 01/10/2013 09:06 AM, richard kappler wrote:
 >>>
 >>> 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.
 >>>
 >>>
 >>>
 >>
 >> Actually one question about those "dozens of related" instances
 >> generated by:
 >> greenwoodTree = Tree()
 >> oakTree = Tree()
 >> ....
 >>
 >> Both, greenwoodTree and oakTree, are derived from Tree class,
 >
 > It's important that we use correct, or at least close terminology. A
 > derived class is VERY different from an instance. greenWoodTree and
 > oakTree are bound to different instances of Tree.
 >
 >> thus receiving the features and also - if so - holding unique values
 >> created in there __init__ generator method
 >
 > __init__ is not a generator. it's simply a method. A method is a
 > function that's defined inside a class
 >
 >> - "self.height", "self.color" and so forth uniquely each.
 >
 > We have to be careful here. Such data can be attributes of an instance,
 > or they can be attributes of a class. Strangely enough, this example
 > has a self.height and tree.height that are both called height, but
 > technically distinct values. I think it's best if you avoid such
 > things, and use unique names till you've got the fundamentals straight.
 >
 >>
 >> But do both share the same function memory space from the class "Tree"?
 >>
 >
 > I wouldn't use the term function memory space, but I'll say this much.
 > Unless you do something tricky, there is only one method Tree.grow, and
 > all instances share that same method. No duplicated memory at all.
 >
 >> I am currently trying also to get my head wrapped around OOP in
 >> general, but not 100% sure so that derived instances use the same
 >> functions (also memory wise speaking) - or are there several
 >> definitions of "grow" ?
 >
 > Not yet. Creating instances don't duplicate any methods. You'd have to
 > do something explicit, and advanced.
 >
 >>
 >> The confusion came somehow when reading about "classmethods" and
 >> "staticmethods" and patterns like Singleton, monostate, borg...
 >> from which I understand only ensure that the "self.height" properties
 >> are shared across multiple instances of a given class?
 >>
 >
 > I'd consider all of those as advanced techniques, and a complete
 > confusion at your present stage. classmethods and staticmethods affect
 > the parameters that a method gets, not whether data properties belong to
 > the class or to the method.
 >
 >> From what I tried out using id() and generating functions in a loop -
 >> the "id(class.function) provided the same result when printed out,
 >> according to that:
 >> 
http://stackoverflow.com/questions/121396/accessing-object-memory-address
 >>
 >> So I assume the functions are shared across thus one decleration has
 >> been made in "Tree" class and all siblings are using that one?
 >>
 >
 > Without showing the code in this loop, I can't comment on what you
 > observed. Don't have any idea what you mean by a sibling. The only
 > meaning I can think of is that two classes each derived from a common
 > base class can be considered siblings. But you don't do any deriving in
 > this email.
 >
 >

I think it's very intuitive (at least, it was to me when I was
learning), to view it as a common starting point for all instances. In
this example it makes sense that all trees (or a general idea of a tree)
starts with a 0 height, and then each particular tree ends up with a
different height after a while.

  -m



-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/



More information about the Tutor mailing list