class methods vs. functions
jacek.generowicz at cern.ch
Thu Jul 15 13:55:50 CEST 2004
"beliavsky at aol.com" <beliavsky at 127.0.0.1:7501> writes:
> 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)?
You're essentially comparing the message-passing paradigm to the
generic function paradigm.
One advantage of message-passing, is that it separates the messages
into namespaces, so you don't have to be as careful about chosing your
One disadvantage of message-passing, is that it separates the messages
into namespaces, so you are less careful about chosing your message
names, and end up confusing your clients. :-)
One advantage of generic functions is that multiple dispatch is much
more natural (dare I say, easier?).
One disadvantage of generic functions is that, if you are not careful,
your code ends up distributed all over the shop.
More information about the Python-list