[Tutor] garbage collection/class question

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


On 12.01.2013 02:19, Mitya Sirenef wrote:
>
> Functions are the same, (called methods), but the self object is
> different for each instance, and represents the instance. Consider that
> since the logic performed by the method is the same (if it wasn't, you'd
> define it as a separate method, right?), there would be no reason to
> make a separate method for each instance. As you know from working with
> functions, the same function may create different output if its
> arguments are different (from another call), but if the arguments are
> the same, it will create the same output (I'm ignoring things like
> random module).
>
> Well, that's why the first argument for a method is 'self', which is
> different for each instance.
>
>>

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.

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.


>
> The core idea of having a class and one or more instances is very
> versatile and powerful, all of the other concepts are much less needed
> especially as you're starting out. For example, you can have a class
> tree and methods and a bunch of tree instances; a classmethod would
> allow you to perform an action without making an instance first, but
> it's not like it's hard to make an instance -- it's just that in some
> cases it's clearer and more straightforward to use a classmethod, like
> Alan pointed out, but if you happen to forget about classmethods, you
> could easily get by without them. The same applies to Borg pattern, etc.
>

Actually Im puzzled with the difference between a classmethod and a 
regular function definition inside a class object.

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()" ?
To what you write, it would make sense like that.

Of course the math module has to be imported first. But thats not the 
point now I guess :)

>  > So I assume the functions are shared across thus one decleration has
> been made in "Tree" class and all siblings are using that one?
>
>
> They are usually called methods if they belong to a class, and yes
> they're shared.
>

Okay, that makes sense - that class functions/methods (confusing with 
that decorator to talk about) are accessible and shared across all class 
instances - and its also what I noticed by using some code like 
following and the id() function.

Dave was asking what code I meant to get the "memory address" on where a 
function is lurking:

---------

class market():
	def __init__(self):
		self.name = 'Market'
	def getMarketPrice(self):
		print self
		print self.__dict__


myInstancesList = []

for x in range(0, 10):
	myInstancesList.append( market() )
	print id(myInstancesList[x].getMarketPrice)

print myInstancesList

---------

Which outputs the same memory reference for the function 
"getMarketPrice" of all instances of the "market" class plus the 
instances inside a list "myInstancesList".

I dont use a dictionary which might be confusing, but makes more sense 
to work with.

Also id() mentioned here: 
http://stackoverflow.com/questions/121396/accessing-object-memory-address
	
And thank you all for the explanations. :)

Jan


More information about the Tutor mailing list