Class Methods

Objects can call methods, those methods are defined in classes.

Methods can modify all variables of an object. This is done with the self keyword.

Because methods can be called from an object (instance), they are sometimes called instance methods.

Related course:
Python Beyond the Basics – Object-Oriented Programming

Example

If you have a class Phone, you can create a new phone object (Nexus).

In this example the class has a method addContact. We call this method in the object, which changes the phones contact list.

The class itself has the methods and variables:

Every object you create has unique variables. If you create 3 objects, all of them have their own unique variables.

In the example above, we create an object Nexus.

class_methods

On this object we can call the methods call() and addContact(). Those methods are defined in the class, but change the objects variables.

Related course:
Python Beyond the Basics – Object-Oriented Programming

In the next article we’ll discuss the self keyword.

Previous Post
Next Post

Leave a Reply