Classes and OOP I

Instance Methods

You have called methods on strings and lists many times: .upper(), .append(). In this lesson you will learn to define your own methods inside a class.


You have been calling methods for a long time. Every .upper() on a string and every .append() on a list is a method call. Now you will define your own.

A method is a function defined inside a class. Its first parameter is always self:

Python

Call a method on an instance using dot notation, just like you call "hello".upper():

Python
Output

What will be the output?

Python

Methods can read the instance attributes through self. That is what makes them powerful.

This method uses self.radius to compute a value:

Python
Output

Methods can also change attributes. This is how objects keep track of their own state over time.

Each call to increment() updates the count:

Python
Output

What will be the output?

Python

Methods can take extra parameters beyond self. Think of them like regular functions with bonus access to the object.

The deposit method takes an amount parameter:

Python
Output

What will be the output?

Python

A method can return a boolean. Useful for checking conditions on the object:

Python
Output

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python