Functions associated with a class.

K

Kurda Yon

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. For me it is easy to understand a method as a one-argument
function associated with a class. 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). If "x" and "y"
belongs to the same class, the "x.calc" and "y.calc" refer to the same
function (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. 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"? Or just calc(x,y)?
In the case of the one-argument functions Pythons automatically decide
which function to call (associated with the first class or with the
second class). Will it be the same in the case of the two-argument
function.

I am not sure that I am clear. If I am not clear, just ask me. I will
try to reformulate my questions.

Thank you.
 
J

John Machin

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. For me it is easy to understand a method as a one-argument
function associated with a class. 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). If "x" and "y"
belongs to the same class, the "x.calc" and "y.calc" refer to the same
function (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. 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"? Or just calc(x,y)?
In the case of the one-argument functions Pythons automatically decide
which function to call (associated with the first class or with the
second class). Will it be the same in the case of the two-argument
function.

I am not sure that I am clear. If I am not clear, just ask me. I will
try to reformulate my questions.

Thank you.

x.calc(y)

where the calc method would (by convention) start

def calc(self, other):

See the very recent thread about "self and other" (or something like
that).
 
H

Hongster

Like what you mentioned, each class has a set of methods and
properties (variables).

Example of a class: Human
Properties of a Human class: height, weight, birthday, occupation, ...
Methods of a Human class: eat(food), move(speed, destination),
sleep(), ...

Methods of a class is just an ordinary function, with an "self"
parameter (by convention). You can define more than 1 arguments for a
function.

Tom = Human()
Dick = Human()
Tom.eat(corn)
Dick.eat(potato)

Tom and Dick are both instances of a Human class. They are not
arguments. "corn" and "potato" contain values that are passed to the
eat() function as argument. The '.' notation is used to indicate which
instance's method/property you are referring to. The 'x' and 'y' you
mentioned are just different instances, they are not arguments.

This is my first post, hopes it helps.
 
B

Bruno Desthuilliers

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top