Main Content
Python - function v method Archive - Originally posted on "The Horse's Mouth" - 2006-10-20 17:42:13 - Graham Ellis
What's the difference between a function and a method? A function is a named piece of code that performs an operation, and a method is a function with an extra parameter which is the object that it's to run on.
Example:
class hotel:
def __init__(self,name,nightly):
self.name = name
self.nightly = nightly
def getweekly(self):
return 7 * self.nightly
def seventimes(amount):
return 7 * amount
# -------------------------------
manor = hotel("Well House Manor",90)
beechfield = hotel("Beechfield",120)
# Calling a METHOD - a function on an object
ma_am = manor.getweekly()
be_am = beechfield.getweekly()
# calling a FUNCTION - less useful code as it
# does not give an implicit link to the data
# that it's on.
rz = seventimes(240)
print ma_am, be_am, rz
Some other articles
Y106 - Object Oriented Python When to check an object type - Python isinstance example Why populate object with values as you construct them? From single block to structure and object oriented programming What is the difference between a function and a method? Simple OO demonstration in C++, comparison to Python Really Simple Class and Inheritance example in Python Spike solution, refactored and reusable, Python - Example this or self - what are they, and what is the difference? (Python) From Structured to Object Oriented Programming. Object oriented or structured - a comparison in Python. Also writing clean regular expressions Moving from scripting to Object Orientation in Python From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) Object Oriented Programming for Structured Programmers - conversion training Tips for writing a test program (Ruby / Python / Java) When should I use OO techniques? Python - a truly dynamic language Introduction to Object Oriented Programming Screw it or Glue it? Access to Object variables - a warning Python class rattling around This article Python makes University Challenge Class, static and unbound variables Y105 - Functions, Modules and Packages From and Import in Python - where is the module loaded from? Embedding more complex code into a named block Nesting decorators Recursion in Python - the classic example What are callbacks? Why use them? An example in Python What is the difference between a function and a method? Reading command line parameters in Python A good example of recursion - a real use in Python Python - even named code blocks are objects Multiple yields and no loops in a Python generator? Python functions - an introduction to how they work Python varables - checking existance, and call by name or by value? Exception, Lambda, Generator, Slice, Dict - examples in one Python program vargs in Python - how to call a method with unknown number of parameters Optional positional and named parameters in Python Default local - a good choice by the author of Python Static variables in Python? Python timing - when to use a list, and when to use a generator Functions are first class variables in Lua and Python Finding all the unique lines in a file, using Python or Perl Python Packages - groupings of modules. An introduction Static variables in functions - and better ways using objects Passing optional and named parameters to python methods Catching the fishes first? Passing parameters to Python functions - the options you have Returning multiple values from a function call in various languages - a comparison Using an exception to initialise a static variable in a Python function / method Python - some common questions answered in code examples Passing a variable number of parameters in to a function / method Program for reliability and efficiency - do not duplicate, but rather share and re-use Optional and named parameters to Python functions/methods Python - access to variables in the outer scope Global and Enable - two misused words! Good example of recursion in Python - analyse an RSS feed Sample code with errors in it on our web site Optional parameters to Python functions Multiple returns from a function in Python Conversion of OSI grid references to Eastings and Northings Dynamic code - Python Optional and named parameters in Python What to do with a huge crop of apples Anonymous functions (lambdas) and map in Python Sharing variables with functions, but keeping them local too - Python Global - Tcl, PHP, Python Python Script - easy examples of lots of basics Returning multiple values from a function (Perl, PHP, Python) A better alternative to cutting and pasting code Function / method parameters with * and ** in Python It's the 1st, not the 1nd 1rd or 1th. Sludge off the mountain, and Python and PHP Python - A list of methods Recursion in Python This article Dynamic functions and names - Python Do not duplicate your code Cottage industry or production line data handling methods Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. Python - block insets help with documentation Python's Generator functions Difference between import and from in Python What is a callback? Code and code maintainance efficiency Call by name v call by value Lambdas in Python Python generator functions, lambdas, and iterators Distance Learning Variable Scope