On Sat, 12 Nov 2005 13:42:37 +0000, Max Erickson wrote: > > Not in python. > > For example, what would you call the following? > > def rsum(n, m): > print n+m > return n+m > > > In python a method is callable attached to an object. A function is a > callable object constructed with a def statement.
That's not *quite* right, because methods are also usually constructed with a def statement as well, and you can attach functions to objects by assignment: py> def spam(n): ... return "spam " * n ... py> class Viking: ... pass ... py> eric_the_red = Viking() py> eric_the_red.spam = spam py> eric_the_red.spam(4) 'spam spam spam spam ' py> type(eric_the_red.spam) <type 'function'> There is surely some technical definition that distinguishes between methods and functions, and between class, instance and static methods. At this level, the technical difference is not important. What is important is that, to a first approximation, functions and methods are the same thing. To a second approximation, functions belong to modules and methods belong to any other object. So in a module that looks like this: X = 1 def frob(x): """Return the frobised value of x.""" return x class Parrot: def frob(self): """Returns the frobised value of self.""" return self the top-level frob is a function and Parrot.frob is a method. Others have suggested that the difference between methods and functions is that methods do something and functions return a result. That's not quite true: both methods and functions can do something ("something" is known as a side-effect, and is *usually* but not always a Bad Idea), and both methods and functions *always* return an object, even if that object is just None. By convention, methods often (but not always) operate by side-effect: L = [1, 4, 2, 7] L.sort() But methods frequently return a value: s = "Hello World" s = s.upper() In fact even L.sort() returns a value: it returns None: who_cares = L.sort() # who_cares is now None; L is now sorted Languages such as Pascal have procedures, which don't return a value, and functions, which do. Python does not have procedures. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list