Method
noun
say it: “METH-ud” /ˈmɛθəd/
A function that belongs to a class, called on an object with a dot, such as cart.add("pen") or name.upper().
What is a method in Python?
A method is a function that belongs to a class. You call it on an object with a dot.
class Thermostat:
def __init__(self):
self.temp = 68
def warm_up(self, degrees):
self.temp += degrees
home = Thermostat()
home.warm_up(4)
print(home.temp) # 72
In a sentence
- “The Thermostat class has a method called warm_up that raises the temperature.”
- “"pizza".upper() calls the string's upper method and gives back "PIZZA".”
Level: Journeyman · More Python terms at this level
Updated