[Tutor] Re: q about method(self)

Emile van Sebille emile@fenx.com
Sat, 31 Aug 2002 09:28:10 -0700


Erik:
> A class method generally takes "self" as its first argument.

It is named self by convention only.  You can call it 'this' if you want.

> I had
> been wondering for some time why this is, since it seems redundant if
> it's there for all class methods.  But I just realized -- you can call
> an object's method without using dot notation in Python, like this:
>
> method(instance_reference, args)

This might also be seen as function(inst_ref, args) which serves to show
that a class method is not really much different from any function.  In this
case, the instance being passed could come from any class.

>
> rather than only like this:
>
> instance_reference.method(args)
>
> I didn't realize that until just now.  But what I'm wondering is,
> why/when would you want to use this form?

Normally, you probably wouldn't.  I imagine if you write utilities or
systems that execute unknown code you might find a need.  ISTM that when you
get to the point of having a class method that turns out to be more useful
as a function, you'd want to refactor it that way.  Time permitting of
course, which may be why you'd use it this other form.  ;-)


Emile van Sebille
emile@fenx.com