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

"method involving two objects" is that possible in Python?

P: n/a
Hi,

I just started to learn Python. I understood how one can create a
class and define a method related with that class. According to my
understanding every call of a methods is related with a specific
object. For example, we have a method "length", than we call this
method as the following "x.length()" or "y.length()" or "z.length()",
where z, y, and z are objects of the class.

I am wandering if it is possible to create a method which is related
not with a single object (as in the previous example) but with a pare
of objects. For example, I want to have a class for vectors, and I
want to have a methods which calculate a dot product of two vectors.
One of the possibilities is to use __mul__ and that I calculated dot
product of "x" and "y" just as "x * y". However, I am wandering if I
can declare a method in a way that allows me to calculate dot product
as "dot(x,y)".

Thank you in advance.
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 27, 2:41*pm, Kurda Yon <kurda...@yahoo.comwrote:
Hi,

I just started to learn Python. I understood how one can create a
class and define a method related with that class. According to my
understanding every call of a methods is related with a specific
object. For example, we have a method "length", than we call this
method as the following "x.length()" or "y.length()" or "z.length()",
where z, y, and z are objects of the class.

I am wandering if it is possible to create a method which is related
not with a single object (as in the previous example) but with a pare
of objects. For example, I want to have a class for vectors, and I
want to have a methods which calculate a dot product of two vectors.
One of the possibilities is to use __mul__ and that I calculated dot
product of "x" and "y" just as "x * y". However, I am wandering if I
can declare a method in a way that allows me to calculate dot product
as "dot(x,y)".

Thank you in advance.
def dot(x, y):
...

class Vector:
__mul__ = dot

or

class Vector:
def __mul__(x, y):
return dot(x, y)

You can refer to the function defined as dot, assuming dot(x, y)
returns some vector z. The first argument (x) will be bound to self in
either case in any Vector instance.
Jun 27 '08 #2

P: n/a
On 27 juin, 23:41, Kurda Yon <kurda...@yahoo.comwrote:
Hi,

I just started to learn Python. I understood how one can create a
class and define a method related with that class. According to my
understanding every call of a methods is related with a specific
object. For example, we have a method "length", than we call this
method as the following "x.length()" or "y.length()" or "z.length()",
where z, y, and z are objects of the class.

I am wandering if it is possible to create a method which is related
not with a single object (as in the previous example) but with a pare
of objects. For example, I want to have a class for vectors, and I
want to have a methods which calculate a dot product of two vectors.
One of the possibilities is to use __mul__ and that I calculated dot
product of "x" and "y" just as "x * y". However, I am wandering if I
can declare a method in a way that allows me to calculate dot product
as "dot(x,y)".
No problem. This is actually called a function. It has the same syntax
as a method, except that:

1/ it's defined outside a class
2/ it doesn't take the instance as first argument.

Here's a simple example applied to multiplication:

def multiply(x, y):
return x * y
HTH.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.