By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,820 Members | 1,103 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,820 IT Pros & Developers. It's quick & easy.

Functions associated with a class.

P: n/a
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.
Jun 30 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Jul 1, 9:44 am, Kurda Yon <kurda...@yahoo.comwrote:
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).
Jul 1 '08 #2

P: n/a
Kurda Yon wrote:
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.
You are very close to being correct. Classes have 'methods' (which can take any
number of arguments) that act like functions and 'attributes' that are normally
stored values in the class instance. I say "normally" because there is an
advanced method of making attributes actually be functions (even though they
appear externally as values).

class foo(object):
def do_square(self, v):
return v*v
# create an instance of our class
a = foo()
# call the do_square method with a value
a.do_square(10)
>>100
# call the do_square method with a different value
a.do_seuare(2)
>>4
or maybe

class bar(object):
def __init__(self):
self.total = 0

def accumulate(self, v):
self.total += v

# create an instance of our new class
b = bar()

# call the accumulate method with several values
b.accumulate(5)
b.accumulate(6)
b.accumulate(7)

# print the value stored in the total attribute of our class instance
print b.total
>>18
Hope this helps.

-Larry
Jul 1 '08 #3

P: n/a
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.

On Jul 1, 7:44*am, Kurda Yon <kurda...@yahoo.comwrote:
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.
Jul 1 '08 #4

P: n/a
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
Jul 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.