Start learning free

LESSON 279 OF 673 · FUNCTIONS & CLASSES

Classes in Python

Lesson

Python is an object oriented programming language. Almost everything in Python is an object, with its attributes and methods.

Class creates a user-defined data structure, which holds its own data members (attributes) and member functions (methods), which can be accessed and used by creating an instance of that class.

Classes are like a blueprint for objects outlining possible behaviors and states that every object of a certain type could have.

Classes are defined via the keyword class, like this:

# Define an empty class Employee
class Employee:
	pass

Objects/Instances

An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. It’s not an idea anymore, it’s an actual employee, like a person named "Andrew" who is 30 years old.

You can have many employees to create many different instances, but without the class as a guide, you would be lost, not knowing what information is required.

After a class is defined, we can create objects of the class type like this:

# Define an empty class Employee
class Employee:
	pass
	
# Create an Employee object
employee = Employee()

Attributes/Properties

A class attribute is a Python variable that belongs to a class and is shared between all the objects of this class. Let's add some attributes to our Employee class:

class Employee:
	name = 'Andrew'
	age = 30

emp1 = Employee()
emp2 = Employee()

print(emp1.name) # prints 'Andrew'

emp2.age = 40
print(emp2.age) # prints 40

We added two properties: the string name and the int age. Then, we created two different Employee objects emp1 and emp2.

The attributes of each object can be accessed and modified using the dot operator (.) like we did above with emp1.name and emp2.age.

The problem here is that emp1 and emp2 start with the same attribute values, name: 'Andrew' and age: 30.

In real life, we want to set the attributes specific to each object when creating it. For example, an employee named Andrew of 30 years old and another one named Mary of 25 years old. Here the constructor comes to rescue.

Constructors

A constructor is a special type of method (function) which is used to initialize the instance members of the class.

In Python the __init__() method is called the constructor and is always called when an object is created.

The constructor takes its first argument as a reference to the instance being constructed known as self and the rest of the arguments are provided by the programmer.

Let's add a constructor to our Employee class:

class Employee:
	def __init__(self, name, age):
		self.name = name
		self.age = age

emp1 = Employee('Andrew', 30) # emp1.name = 'Andrew', emp1.age = 30
emp2 = Employee('Mary', 25) # emp2.name = 'Mary', emp2.age = 25

The "self"

Class methods must have an extra first parameter in the method definition. We do not give a value for this parameter when we call the method, Python provides it.

This extra parameter, known as self, is a refference to the object we called the method on. We use self to access and manipulate the attributes of that object, like we did above with self.name = name.

Basically, when we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.

Member functions

Member functions are functions that belong to the class:

class Employee:
	def __init__(self, name, age):
		self.name = name
		self.age = age
		
	def printName(self):
		print(self.name)
		
emp1 = Employee('Andrew', 30)

emp1.printName() # prints 'Andrew'

emp2 = Employee('Mary', 25)

emp2.printName() # prints 'Mary'

As you can see, we access functions just like we access attributes: by creating an object of the class and using the dot syntax (.).

The function call uses the attributes of the object it was called on. That's why the first printName() prints 'Andrew' and the second prints 'Mary'.

Calling member functions from other member functions

One more important thing to know is that when we call a member function from an other member function, we should use the self keyword:

class Employee:
	def __init__(self, name, age):
		self.name = name
		self.age = age
		
	def printName(self):
		print(self.name)
		
	def printEmployee(self):
		self.printName() # here
		print(self.age)
		
emp = Employee('Andrew', 30)

emp.printEmployee() # prints 'Andrew', 30

Here, inside printEmployee(self) we call printName function like this: self.printName(). We always do it like this in these scenarios.


Assignment
Follow the Coding Tutorial and let's write some classes.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 7
    Ctrl + Enter to check