Python Class

Summary: in this tutorial, you’ll learn about Python classes and objects and how to define a new class.

Understanding objects and classes

In Python, every value is an object. For example, an integer, a string, or a boolean is an object. Also, a list, a tuple, a dictionary, or set, etc., is an object.

Programs manipulate these objects by:

  • Performing computation with them.
  • Or requesting them to perform some operations.

More specifically, an object has a state and a collection of actions that it can perform.

The state of an object represents the information that the object knows about itself. In addition, an object has actions that it can perform.

Python class - Object

Suppose that you need to develop an HR application. The first objects you may come up with are the personal information that the application needs to manage.

A person can have a first name, last name, and age. The first name, last name, and age are the attributes of a person object. The person objects can have actions like greeting to others:

To model objects in Python, you use classes. A class is a blueprint for similar objects. From a blueprint, you can create many objects with the same attributes and actions.

Creating an object from a class means instantiating an instance of that class. Therefore, objects are instances of a class.

Defining a class

To define a class in Python, you use the class keyword followed by a name and a colon like this:

class Person: pass
Code language: Python (python)

By convention, you use capitalized names to refer to classes in Python. If the class name contains multiple words, you use the CamelCase format, for example OfficeManager, FulltimeEmployee, etc.

Since the Person class is incomplete, you need to use the pass statement to indicate that you’ll add more code to it later.

To create an instance of a class, you use the class name with parentheses like this:

john = Person() jane = Person()
Code language: Python (python)

In this example, we create two instances of the Person class called john and jane. Since the Person class is empty, these objects aren’t useful yet.

When you print out these objects, you’ll see their memory addresses:

class Person: pass john = Person() jane = Person() print(john) print(jane)
Code language: Python (python)

Output:

<__main__.Person object at 0x0000023CCA076880> <__main__.Person object at 0x0000023CCA076E50>
Code language: Python (python)

From the memory addresses 0x0000023CCA076880 and 0x0000023CCA076E50, you can check if they are the same object. The same objects will have the same memory address.

Instance methods

As mentioned earlier, an object has actions. To model actions in Python, you use functions.

When a function belongs to a class, it’s called a method. Everything you learned about functions applies to methods too.

The following adds a method called greet() to the Person class:

class Person: def greet(self): print('Hi')
Code language: Python (python)

The greet() method looks like a regular function except it accepts a parameter called self, which you’ll learn more about it later.

To call a method, you use the following syntax:

object_name.method_name()
Code language: Python (python)

The following example creates an instance of the Person class called john and call the greet() method on the john object:

john = Person() john.greet()
Code language: Python (python)

If you execute the following program, you’ll see a message on the screen:

class Person: def greet(self): print('Hi') john = Person() john.greet()
Code language: Python (python)

Output:

Hi
Code language: Python (python)

The self parameter

When you define a function with a parameter, you need to pass an argument. Otherwise, you’ll get an error. For example:

def greet(message): print('Hi') greet()
Code language: Python (python)

Error:

TypeError: greet() missing 1 required positional argument: 'message'
Code language: Python (python)

In the Person class, the greet() method has the self parameter. However, when calling it, you don’t pass any argument into it. And it works perfectly fine.

The reason is that when you call a method on an object, Python implicitly passes that object to the method as the first argument.

The self is the instance of the class on which the method is called. In this example, when you call the greet() method on the john object:

john.greet()
Code language: Python (python)

…the self is the john object.

Technically, it’s equivalent to the following:

john.greet(john)
Code language: CSS (css)

The following proves that john and self objects are the same object since they reference the same memory address:

class Person: def greet(self): print(self) print('Hi') john = Person() john.greet() print(john)
Code language: Python (python)

Output:

<__main__.Person object at 0x0000016E77CC6880> Hi <__main__.Person object at 0x0000016E77CC6880>
Code language: Python (python)

As a rule of thumb, all instance methods of a class must have at least one argument (self) which is the instance of the class.

Python uses parameter name self by convention only. Therefore, you can name it whatever you like. However, it’s a good practice to call it as self for consistency.

Instance attributes

Through instance methods, you can create instance attributes with the following syntax:

self.attribute_name = value
Code language: Python (python)

The following adds two instance methods called set_age() and get_age() to the Person class:

class Person: def greet(self): print('Hi') def set_age(self, age): self.age = age def get_age(self): return self.age
Code language: Python (python)

The set_age() method creates the age attribute as an instance attribute. And it assigns the age argument to the age attribute:

self.age = age
Code language: Python (python)

The get_age() method simply returns the value of the age attribute.

Note that the set_age() and get_age() methods are often referred to as setter and getter.

The age is called an instance attribute because its value associates with a specific instance.

For example, the following creates two instances of the Person class:

john = Person() john.set_age(25) print(john.get_age()) jane = Person() jane.set_age(20) print(jane.get_age())
Code language: Python (python)

Output:

25 20
Code language: Python (python)

Both john and jane instances have separate age attributes associated with them. The john.age returns 25 while the jane.age returns 20.

Summary

  • A class is a blueprint for creating objects.
  • An object is an instance of the class.
  • An object has attributes and methods.
  • An instance method always has the self parameter that references the instance which calls the method.
  • Instance attributes are bound to a specific instance of the class.
Did you find this tutorial helpful ?