[Tutor] garbage collection/class question

Jan Riechers janpeterr at freenet.de
Sat Jan 12 11:55:17 CET 2013


On 12.01.2013 11:24, Alan Gauld wrote:
> On 12/01/13 08:09, Jan Riechers wrote:
>
>> So to rephrase what you and also other wrote:
>> By setting "oakTree = Tree()" I create a new "Tree()" class instance.
>> Now calls to "oakTree.grow()" access functions of the Tree class, by
>> traversing to it's "Superclass" Tree.
>
> No, they traverse to its Tree class. Superclasses are only involved when
> you use inheritance. Consider:
>
> class Tree:
>     def __init__(self,height=0):
>       self.height = height
>
>     def getHeight(self):
>        return self.height
>
> def EverGreen(Tree):    # subclass of Tree
>     def __init__(self, height=0, color='green'):
>         Tree.__init__(self,height)
>         self.color = color
>
>     def getColor(self):
>         return self.color
>
>
> t = Tree()
> print t.getHeight()     # calls Tree.getHeight
>
> g = EverGreen()
> print g.getHeight()    # calls Tree.getHeight by searching superclass
> print g.getColor()     # calls EverGreen.getColor
>
> So the superclass is only used in the case of g.getHeight()
> Python looks for getHeight in the class of EverGreen and can't find it.
> So it looks in the superclass Tree to see if it can find getHeight there.
>

Actually I miss the terminology but that was what I thought to describe 
- also referring to your initial post on my question. If a class is 
based on a super class, the message lookup traversal goes from child to 
superclass, which now makes sense.


>> The "self" then, which also is used in the Superclass Function only
>> tells, work with the "own" (self) values of the class instance, instead
>> of the values of the class itself.
>>
>> I guess that's right.
>
> Maybe, I'm not sure what you mean. self is a reference to the instance
> invoking the method.
>

Given:

class Market():
	def __init__(self):
		self.traders = 0

	def addTrader(self):
		self.traders += 1

instanceMarket = market()
print instanceMarket.traders # 0
instanceMarket.addTrader()
print instanceMarket.traders # 1

So the value of "traders" is unique to the instance, but the class 
function "addTrader", refers to the instance on which the function is 
invoked on (using self), but is declared only once for all instances 
based on "market". That also does now make sense. :)

>> Actually Im puzzled with the difference between a classmethod and a
>> regular function definition inside a class object.
>
> All methods are functions defined inside classes. "regular functions"
> are by definition NOT defined in a class. regular functions require no
> lookup mechanism and do not have a "magic" first parameter like self.
>
>> Does the classmethod just mean that I can use the class "math" and call
>> "math.random()" without creating an instance of math before using
>> "random()" ?
>
> Maybe, if math were a class. But math is actually a module which is
> different to a class.
>

I think again the terms are mixing things up.
I tried that with the "classmethod" decorator and ended up with 
something like this:
class market():
	__balance = 500

	@classmethod
	def getBalance(cls):
		print cls.__balance


In this case now, from what was explained and what meant to say,
market.getBalance() can now called without creating an instance of that 
class (like a module, even so it is not.)

So calling:
market.getBalance() # --> 500

>> Okay, that makes sense - that class functions/methods (confusing with
>> that decorator to talk about)
>
> The decorator is only used for classmethods not for instance methods.
> Most of the time you don't need to use classmethods you only need
> instance methods.
>
Sorry again for the long post and mixing up the wordings, I guess I have 
to practice and read a bit more, but I understand now more of the OOP 
concept and inner workings than before - so it helped already :)

Jan




More information about the Tutor mailing list