Functions associated with a class.

Hongster expertleong at gmail.com
Mon Jun 30 22:59:09 EDT 2008


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... at yahoo.com> 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.




More information about the Python-list mailing list