Python Classes & Objects

code guest
Python Journey
Published in
3 min readAug 10, 2019

In Python, every piece of data is first stored as a Python object with has a data type (eg. integer).

So 123 is an integer object while “hello world” is a string object.

Next, different data types support different operations. (eg. mathematical computation for integers — see earlier article on data types if unsure)

Introducing the concept of objects which explains how all of that works.

In fact, it is not just data information, every single thing in Python like a function or a class (to be introduced later) is an object.

And therefore, in our Python code, we might work with many different string objects, integer objects and so on.

One thing that unite all string objects, or unite all integer objects, is that they belong to the same class.

A class is a blueprint / template that makes objects work the way they do — like how string objects work as text or how integer objects take part in mathematical computations.

Specifically, a class is a special data type which defines how to build a certain kind of object.

In writing Python code, we deal with lots of objects but they mostly belong under the most common classes. (credits: undraw.co)

To understand how classes work, we need to know that every class consists of three components — a name, data fields and code.

  • integer class— name: integer, data fields: to hold numerical information and other information, code: perform mathematical computations etc
  • string class — name: string, data fields: to hold text information and other information, code: perform string manipulations etc

Classes are templates for objects in that classes define what kind of information its object stores and the code that the object can run — hence, 123 as an object of the integer class stores numerical information 123 and can perform mathematical computations.

It shouldn’t surprise you now that Python is known as a class-based object-oriented programming language.

References

Summary

  • Every thing in Python is an object
  • Objects belong to classes
  • Classes have name, data fields and code which in turn defines the information stored and code executable in its objects.

Quiz

Which of the following options are incorrect?

a) An object is a data type

b) “hello” and “world” are two string classes

c) Different data types support different operations because they are of different classes

d) 123 and 321 are two integer objects that have different data stored but can run the same code

Quiz explanation

a) Data types are implemented by classes in Python. In essence, a class (eg. integer class) defines a data-type (eg. integer data type). Follow this link for more info.

b) “hello” and “world” are two objects of the same class

c) Option (c) is accurate — see answer for option (a) above

d) Option (d) is accurate — 123 is an integer object storing numerical data 123 while 321 is an integer object storing numerical data 321. They both can perform the same numerical calculations as defined by the integer class.

--

--