How to make a function associated with a class?

Kurda Yon kurdayon at
Tue Jul 1 22:43:45 CEST 2008


I have a class called "vector". And I would like to define a function
"dot" which would return a dot product of any two  "vectors". I want
to call this function as follow: dot(x,y).

Well, I can define a functions "dot" outside the class and it works
exactly as I want. However, the problem is that this function is not
associated with the class (like methods a method of the class).

For example, if I call "x.calc()" or "y.calc()", python will execute
different methods if "x" and "y" belongs to different classes. I want
to have the same with my "dot" function. I.e. I want it calculates the
dot product ONLY IF the both arguments of that function belong to the
"vector" class.

Is it possible?

Thank you in advance.

More information about the Python-list mailing list