class methods vs. functions beliavsky at
Wed Jul 14 20:57:12 CEST 2004

What are the pros and cons of defining a method of a class versus defining
a function that takes an instance of the class as an argument? In the example
below, is it better to be able to write 
z.modulus() or modulus(z)? Is there a difference between Python and C++ in
this regard?

from math import sqrt
class xy:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def modulus(self):
        return sqrt(self.x**2 + self.y**2)

def modulus(z):
    return sqrt(z.x**2 + z.y**2)
z = xy(3,4)
print z.modulus()
print modulus(z)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

More information about the Python-list mailing list