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:
Call a method on an instance using dot notation, just like you call "hello".upper():
What will be the output?
Methods can read the instance attributes through self. That is what makes them powerful.
This method uses self.radius to compute a value:
Methods can also change attributes. This is how objects keep track of their own state over time.
Each call to increment() updates the count:
What will be the output?
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:
What will be the output?
A method can return a boolean. Useful for checking conditions on the object:
What will be the output?
What will be the output?
What will be the output?