Functions associated with a class.

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Jul 1 05:41:38 EDT 2008


Kurda Yon a écrit :
> Hi,
> 
> I start to learn the object oriented programing in Python. As far as I
> understood, every class has a set of corresponding methods and
> variables.

Every object has a set of attributes. Some of these attributes are 
methods (which are thmeselves objects too), some are not. Some are in 
fact attributes of the class of the object accessed thru the instance 
(mostly, the methods), some are instance-specific (mostly, non-methods).

> For me it is easy to understand a method as a one-argument

Why "one-argument" ? A method can take as amny arguments as necessary.

> function associated with a class.

s/class/object/. While methods are usually attributes of the class 
(accessed thru an instance), nothing prevents you from adding methods on 
a per-instance basis. This set aside, truth is that methods are just 
thin wrapper objects around a function, an instance and/or a class. The 
responsability of the method object is to delegate call to it's function 
object, inserting the instance (or the class in the case of a 
classmethod) as the first argument.


> For example, if I call "x.calc" and
> "y.calc" and if "x" and "y" belongs to different classes I, actually,
> call to different function (the first one is associated with the first
> class and the second one with the second class).

Here again, it's a bit more complex. But this is a correct enough 
description of the most common case.

> If "x" and "y"
> belongs to the same class, the "x.calc" and "y.calc" refer to the same
> function

Usually, yes, but not necessarily:

def yadda(obj):
    print "yadda(%s)" % obj

class Bar(object):
    def foo(self):
        print "foo(%s)" % self

b1 = Bar()
b1.foo()

b2 = Bar()
b2.foo()

b2.foo = yadda.__get__(b2, type(b2))
b2.foo()


> (but called with different arguments ("x" and "y",
> respectively)).
> 
> In the above described case we have one-argument function. But what
> should we do if we one two have a two-argument function.

class Baaz(object):
     def gluuk(self, other):
         print "gluuk(%s, %s)" % (self, other)

> For example,
> we want to have a method "calc" which take two objects and returns one
> value. How do we call this method? Like "x&y.calc"?

class X(object):
    def calc(self, other):
        # this is silly since we don't use neither
        # self nor other, but you get the idea
        return "one value"

Nope.

> Or just calc(x,y)?

That's one of the possibilities, but it's not a method anymore, just a 
plain function. The others solutions are either "x.calc(y)" or 
"y.calc(x)". Now which one is the right one depends on what calc do and 
how it relates to the respective classes of x and y. If the operation is 
really a responsability of one of the classes, then it should be a 
method of this class. Else it's probably better to stick to a plain 
function.

> In the case of the one-argument functions Pythons automatically decide
> which function to call

Actually, x.calc() is (usually) a shortcut for type(x).calc(x), so you 
could say that in fact *you* make the decision of which function will be 
called !-)

> (associated with the first class or with the
> second class). Will it be the same in the case of the two-argument
> function.

It can't. OO polymorphic dispatch is (more or less by nature) a single 
dispatch mechanism. If you want a multiple dispatch (ie: dispatch on an 
arbitrary number of arguments) system, there are a couple packages 
providing this kind of features, but nothing builtin. Also, for a double 
dispatch mechanism, you can have a look at the Visitor design pattern.

HTH



More information about the Python-list mailing list