Files
freeCodeCamp/guide/english/python/object-oriented-programming/methods/index.md
Glafs 9a72acf7ac Added information to python method article (#27410)
* Added information to python method article

Added information over methods in OOP in python.

* Corrected mistakes in python methods guide

Added the py syntax highlighting and corrected some grammar.

* fix: remove stub info
2019-06-27 18:51:42 -07:00

991 B

title
title
Methods

Methods

In object oriented programming a function within a class or an object is named a method. Imagine you have a cat object and inside the class there is a funcion named scratch, you can now use the cat's scratch method by calling it in the following way:

cat.scratch(somePerson)

Different kind of methods

There are different types of methods you can define in a class, some of them are listed here.

The constructor, a method that is executed when the object is created:

class Cat:
  def __init__(self, name): // Constructor which recieves a name
    self.name = name

Standard methods:

class Cat:
  name = "Bob"
  
  def scratch(self, person): // Standard method, recieves person to scratch
    print(self.name + " scratched " + person)

The destructor, a method that is executed when the object is deleted by the garbage collector.

class Cat:
  name = "Bob"
  def __del__(self):
    print(name + " is no more.")