🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

Understanding How Python Class Works

Reading time 3 min
Published Oct 1, 2019
Updated Oct 3, 2019

TL;DR – Python class is a blueprint for a kind of object. In Python, you can instantiate classes to create new objects.

What is a Python class?

Unlike a procedural programming language, any coding done in Python revolves around objects. In some object-oriented languages, objects are just basic chunks of data and attributes. However, in Python, they consist of functions as well as information.

In short, a Python class is for defining a particular type of object. Because Python objects can have both function and data elements, Python classes define what methods can be used to change the state of an object. They also indicate what attributes the object can have.

Creating a Python class definition

It’s not hard to define Python class. To do so, you’ll need the class keyword:

Example
class Example:
  "A basic example class"
  variable = 123

If you run the above code in a Python environment, you’ll find you can call Example.a to return an integer value. This is an example of a class for data-only objects, but it’s equally easy to define a class that returns a function object by adding the def keyword to your code:

Example
class Example:
  "A basic example class that returns a function object"
  def b(self):
    return "this is an example class"

Instantiating a Python class object

With the above Python class examples, you can create a class object through attribute referencing using Example.a and Example.b respectively.

It’s also possible to instantiate a class object via function notation. Simply set a variable equal to the class in the same way you’d call a function with no parameters:

Example
class Example:
  "A basic example class that returns a function object"
  def b(self):
    return "this is an example class"

c = Example()

With this, a new class object will be instantiated and attributed to local variable c.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

The constructor method for class object instantiation

It’s possible to simulate the constructor of a class in Python using the __init__() method. This is a helpful way to initialize attributes for all objects in a given class:

Example
class Square:
   def __init__(self, length, width):
       self.length = length
       self.width = width
   def area(self):
       return self.width * self.length
r = Square(20, 2000)
print("Rectangle Area: %d" % (r.area()))

This Python class example results in the following output:

Example
Rectangle Area: 40000

Note: Python uses a default constructor for any class where you don’t add your own.

Working with complex class objects

Of course, a key reason to use classes in Python is that doing so allows you to create custom object types. With these, you can instantiate variables that represent things like co-ordinates or other complex data points:

Example
class PartsColor:
  "Creates a class"
  def __init__(self):
    "Create a new color for each part"
    self.hood = "Blue" 
    self.wheels = "Red"
    self.doors = "Green"
e = PartsColor() # Instantiate two objects to represent points
f = PartsColor()        

print(e.hood, f.wheels, e.hood, f.wheels, e.doors, f.doors)  # Assign each point a unique location

This will output:

Example
Blue Red Blue Red Green Green

Python class: useful tips

  • You can use Python functions like getattr(obj,name,default) to check and modify the attributes of an object even after they have been initialized through a Python class.
  • In Python classes, there’s a big difference between inheritance and composition. Inheritance transfers attributes and methods used in one class to another. Composition means that a base class isn’t inherited from.