Python Classes and Objects

Python is an object oriented programming language.

Classes and objects are the two main aspects of object-oriented programming.

A class creates a new type where objects are instances of the class.

Did you know?

In Python, everything is an object: integers, strings, lists, functions, even classes themselves.

Create a Class

You can create a class using the keyword class.

Example: Create a class ‘car’ with 2 attributes

class car:
    brand = 'Ford'
	model = 'Mustang'

Create an Object

To create an object (instance) of a class, you use the class name, followed by parentheses.

Example: Create an object of class ‘car’

C = car()

Class Attributes

Variables within a class are known as class attributes.

Example: ‘brand’ and ‘model’ are class attributes

class car:
    brand = 'Ford'
	model = 'Mustang'

Another term for class attributes is fields.

Access Attributes

You can access attributes by creating an object of the class, and by using the dot notation .

Example: Access attributes by using the dot notation

C = car('Ford', 'Mustang')

print(C.brand)	# Ford
print(C.model)	# Mustang

Modify Attributes

You can also modify attribute values:

Example: Modify attributes ‘brand’ and ‘model’

C.brand = 'Chevrolet'
C.model = 'Camaro'

The __init__() Method

A __init__() method in Python is a special method that is used to initialize objects.

This method runs automatically each time an object of a class is created.

You can use it to set default values for object attributes or perform operations that are necessary before the object is created.

Example: Use __init__() method to set values for object attributes

class car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

C = car('Ford', 'Mustang')

print(C.brand)  # Ford
print(C.model)  # Mustang

Object Methods

Object methods are nothing but functions, defined inside a class.

They are used to perform operations with the attributes of an object.

Example: Create an object method to display ‘brand’

class car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def displayBrand(self):

C = car('Ford', 'Mustang')

C.displayBrand()    # Ford

The self Parameter

The self parameter represents the instance of the class.

It is used to create and access the object attributes (specific to each object) of the class.

This parameter doesn’t have to be called self, you can call it whatever you want, but it is standard practice, and you should probably stick with it.

In addition, it must be the first parameter of any method in the class.

Example: Use ‘xyz’ instead of ‘self’

class car:
    def __init__(xyz, brand, model):
        xyz.brand = brand
        xyz.model = model

    def displayBrand(xyz):

C = car('Ford', 'Mustang')

C.displayBrand()    # Ford

Class Attributes Vs Instance Attributes

Instance attribute:

Instance attribute is specific to each object. Every object of that class has its own copy of that variable.

Any changes made to the variable don’t reflect in other objects of that class.

Class attribute:

Class attribute is the same for all objects. And there’s only one copy of that variable that is shared with all objects.

If changes are made to that variable, they will reflect in all other objects.

Example: Unlike instance attribute (brand), class attribute (wheels) is same for all objects

class car:

	wheels = 4	# class attribute

	def __init__(self, brand):
		self.brand = brand	# instance attribute

C1 = car('Ford')
C2 = car('Chevrolet')

print(C1.wheels)	# 4
print(C1.brand)		# Ford

print(C2.wheels)	# 4
print(C2.brand)		# Chevrolet

So, while each car has a unique brand, every car has 4 wheels.

Private Attributes

It is possible to have outside code manipulate object attributes.

To make attributes private, so only methods in the object can manipulate them, prepend the attribute with two underscores, e.g. __myvariable.

Example: Make instance attributes ‘brand’ and ‘model’ private

class car:
    def __init__(self, brand, model):
        self.__brand = brand
        self.__model = model

    def displayBrand(self):

C = car('Ford', 'Mustang')

print(C.brand)      # AttributeError
C.displayBrand()    # Ford

Delete Attributes and Objects

To delete any object attribute, use the del keyword.

Example: Delete an attribute

del C.brand

You can delete the object completely with del keyword.

Example: Delete the object completely

del C