3 Replies - 13679 Views - Last Post: 07 October 2011 - 07:03 AM Rate Topic: -----

#1 cryptoxic   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 22-January 11

Python built-in functions vs built-in methods

Posted 06 October 2011 - 09:41 PM

Hello all!
What is the difference between methods and functions in Python? As I understand it, methods are class functions.
Is This A Good Question/Topic? 0
  • +

Replies To: Python built-in functions vs built-in methods

#2 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python built-in functions vs built-in methods

Posted 07 October 2011 - 04:34 AM

The term "method" and "function" often are used in place of each other, especially across different languages.

As I understand it, difference between methods and functions are that methods are included in a class and the output can change according to the state of that class. Functions have the same output given the same input each time.

class Message:
     #Constructor
     def __init__(self, message):
         self.message = message

    def printMessage(self):
        print self.message



Compared to a method outside a class:

def printMessage():
    message = "I'm a stand-alone function!"
    print message



The output from the function printMessage will always be the same, but the method printMessage is dependant on the class:

>>> instance1 = Message("I'm an instance1 message!")
>>> instance2 = Message("I'm an instance2 message!")
>>> printMessage()
I'm a stand-alone function!
>>> instance1.printMessage()
I'm an instance1 message!
>>> instance2.printMessage()
I'm an instance2 message!



There are things called "class methods" and "static methods" which are like stand-alone functions but within a class. But usually, the methods within a class depend on state.

This post has been edited by Simown: 07 October 2011 - 04:37 AM

Was This Post Helpful? 3
  • +
  • -

#3 cryptoxic   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 22-January 11

Re: Python built-in functions vs built-in methods

Posted 07 October 2011 - 05:20 AM

View PostSimown, on 07 October 2011 - 04:34 AM, said:

The term "method" and "function" often are used in place of each other, especially across different languages.

As I understand it, difference between methods and functions are that methods are included in a class and the output can change according to the state of that class. Functions have the same output given the same input each time.

class Message:
     #Constructor
     def __init__(self, message):
         self.message = message

    def printMessage(self):
        print self.message



Compared to a method outside a class:

def printMessage():
    message = "I'm a stand-alone function!"
    print message



The output from the function printMessage will always be the same, but the method printMessage is dependant on the class:

>>> instance1 = Message("I'm an instance1 message!")
>>> instance2 = Message("I'm an instance2 message!")
>>> printMessage()
I'm a stand-alone function!
>>> instance1.printMessage()
I'm an instance1 message!
>>> instance2.printMessage()
I'm an instance2 message!



There are things called "class methods" and "static methods" which are like stand-alone functions but within a class. But usually, the methods within a class depend on state.

Thank you for your reply. It made things much clearer for me
Was This Post Helpful? 0
  • +
  • -

#4 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python built-in functions vs built-in methods

Posted 07 October 2011 - 07:03 AM

You're welcome. I'm glad it made things clearer :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1