On 11/01/13 19:41, Jan Riechers wrote:

class Tree(object):
     height = 0

     def grow(self):
         self.height += 1


Actually one question about those "dozens of related" instances
generated by:
greenwoodTree = Tree()
oakTree = Tree()
....

Both, greenwoodTree and oakTree, are derived from Tree class,

Ok, a small terminology issue. They are not derived from Tree, they are Trees. They are two instances of the same class. Derived is used to suggest that they are sub classes of Tree via inheritance, a whole different discussion.

receiving the features and also - if so - holding unique values created
in there __init__ generator method - "self.height", "self.color" and so
forth uniquely each.

But do both share the same function memory space from the class "Tree"?

Yes. each instance holds a reference to its class. The class holds the function definitions. That's why you need a self parameter, it tells the class method definition which instance is being operated upon.


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

instances all refer to their class which holds the methods for the class.

Where inheritance is concerned the class then holds a further reference to its parent class and Python looks for the earliest method definition as it traverses that class hierarchy. In other words if the object class doesn't have a method definition to match the message name then it looks in the superclass's definition for such a method, and if not there it looks in the superclass's superclass and so on until it finds a match. It is this search that makes methods different to simple functions.


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?

Not quite. Class attributes are common across *all* instances of the class. Class methods are, like instance methods, stored in the class but do not need an instance to exist to call them. In fact class methods can be used as "factories" to create instances. This is a common pattern where you want a way to recover stored classes from a database or file or over a network connection. For example:


foo = MYClass()   # create a brand new instance in memory
id = foo.store(DB)  # save it in database
del(foo)   # get rid of the object in memory
foo2 =- MYClass.restore(db, id)    #  new instance loaded from DB.
bar = MYClass.load(someSocket) #instance read from network port

store() is an instance method that stores the instance data in a database. restore() is a class method that resurrects that instance by loading it from the database

load() is a class method that reads the data on a network port and interprets it as instance data and returns a new instance with those values..

 From what I tried out using id() and generating functions in a loop -
the "id(class.function) provided the same result when printed out,

Yes, because they are shared.

But note that this is all implementation detail, there are other OOP languages that do things differently. Python's approach is the most common but not guaranteed. In fact I'm not even sure if it's guaranteed across different Python implementations!

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to