GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • Python Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Methods in Python - SyntaxDB - Python Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Print
      • User Input
      • Commenting
      • Read from Text File
      • Write to File
      • Import Module
    • Variables
      • Variable Declaration
      • Lists
      • List Comprehensions
      • Tuples
      • Dictionaries
      • Sets
    • Control Flow
      • If Statement
      • For Loop
      • While Loop
      • Ternary Operator
      • Try Catch Block
      • With Statement
      • Generators
    • Functions
      • Functions
      • First Class Functions
      • Lambda
      • Map
      • Filter
    • Object Oriented Programming
      • Class Declaration & Structure
      • Class and Instance Variables
      • Constructor
      • Instantiation
      • Methods
      • Inheritance
Categories
Language
Python
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 3.4.3

Categories

  • User Program Communication
    • Print
    • User Input
    • Commenting
    • Read from Text File
    • Write to File
    • Import Module
  • Variables
    • Variable Declaration
    • Lists
    • List Comprehensions
    • Tuples
    • Dictionaries
    • Sets
  • Control Flow
    • If Statement
    • For Loop
    • While Loop
    • Ternary Operator
    • Try Catch Block
    • With Statement
    • Generators
  • Functions
    • Functions
    • First Class Functions
    • Lambda
    • Map
    • Filter
  • Object Oriented Programming
    • Class Declaration & Structure
    • Class and Instance Variables
    • Constructor
    • Instantiation
    • Methods
    • Inheritance

Object Oriented Programming

Methods in Python

Methods are functions declared within a class. There are three types: instance, class, and static.

Instance methods have different implementation across all instances. They are called in relation to the current instance.
Class methods have the same implementation across all instances. They are called in relation to the instance passed in.
Static methods have the same implementation across all instances. They are not tied to a class instance.


Syntax
class ClassName(object):
    #instance methods always take self as the first parameter
    def instance_method(self, passed_params):
        #code to execute

    #the constructor is an instance method
    def __init__(self, passed_params):
        #assignments

    #class methods always take a class instance as the first parameter
    @classmethod
    def class_method(cls, passed_params):
        #code to execute

    @staticmethod
    def static_method(passed_params):
        #code to execute

class_instance = ClassName()
#calling instance and class methods
class_instance.instance_method()
#calling static methods
ClassName.static_method()

Notes

Like variables, leading double underscores can be used to indicate that access level of the method is meant to be private.


Example
class Dog(object):
    #class attribute
    legs = 4

    #instance methods
    def __init__(self, breed):
        self.breed = breed

    def get_breed(self):
        return self.breed

    @classmethod
    def get_legs(cls):
        return cls.legs

    @staticmethod
    def make_noise():
        return "*bark*!"

new_dog = Dog("Golden Retriever")
print ("I got a new dog. It is a {}".format(new_dog.get_breed()))
print ("It goes {}".format(Dog.make_noise()))

See Also
Related

Constructors
Class Declaration and Structure
Functions

Documentation

Classes - Python Documentation

Add to Slack

< Instantiation   |   Inheritance >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.