Python Object Oriented Programming - Class,Attributes and Methods

| Page Views: 35

Objects in Object Oriented Programming are models for things or items in the real world.

They are a collection of data and associated behaviors.

Object Oriented Programming is a paradigm used to model complex systems by describing a collection of interacting objects via their data and behavior.

Objects are described using classes. They are the blueprints for creating an object.

This creating of an object is also called instantiating a class,meaning creating an instance of a class.

Creating Classes in Python

To create a class we use the class keyword, followed by the identifier or name of the class, then a colon.

class Nebular:

The class name or identifier must follow standard Python variable naming specifications:

  1. It must begin with a letter or underscore.
  2. It can be comprised only of
    • Letters.
    • Underscores.
    • Numbers.

In Python we use indentation to delimit classes and not braces or brackets.

In the simple program we've written, we've used the pass keyword to pass, or indicate that no further action needs to be taken.

Instantiating a Class

The example we declared above is as simple as it can get. However, it's full class and therefore is eligible for instantiation.

Instantiation is the creation of an object of a given class.

To instantiate a class, you just follow the class name with a pair of parantheses.


Then we can even print that instance:

class Nebular:



<__main__.Nebular object at 0x009B5EB0>

Everything is working perfectly. That result is just python telling us the which class our object is created from and the memory address the object resides at.

Adding Class Attributes

The basic class is we created above isn't very interesting and helpful as it didn't have any data.

However, python is such a flexible language that we don't have to go back to our class and make changes if we don't want.

We can just use the dot notation to assign attributes to our instances.

class Nebular:
# instantitae Nebular and assign attributes
orion.fullName='Orion Nebular'
orion.category='HII Region'

bernad68.fullName='Bernad 68'
bernad68.category='Dark Nebular'

# Print out attributes


Orion Nebular HII Region
Bernad 68 Dark Nebular

Adding Class Methods

Objects, we had agreed, define both data and behavior.

These behaviors are normally implemented via methods. Then different objects can interact by calling each others methods.

The methods themselves manipulate data.

class Nebular:
    # instance methods
    def showIntro(self):
        print('Nebulars are normally considered the pillars of creation.')
    def showDescription(self, description):

# instantitae Nebular and invoke instance method
horseHead.showDescription('Horse Head Nebular is one of the most famous nebulars. This due to those beautiful images from Hubble.')


Nebulars are normally considered the pillars of creation.
Horse Head Nebular is one of the most famous nebulars. This due to those beautiful images from Hubble.

In Python, methods have the same syntactical definition as functions:

  1. They start with def keyword.
  2. They are followed by method identifier or name.
  3. Then a set of parenthesis containing parameter list.
  4. They are terminated by a semicolon.
  5. Then the statements to constitute the body of the method are indented.

Passing of self Keyword in Methods

We talked about how methods are syntactically similar to functions. However, there is one big difference between them:

All methods have one required argument conventinallu called self. But you can call it this or anything you like. However, alot of programmers prefer self.

What is this self argument? Well it is a reference to the object that the method is being invoked on. Remember methods define behaviors for objects.

Well this object is what is being referenced as self. With the self keyword, we can access the current object's atrributes and methods using the dot notation.

class Nebular:
    def showName(self):

helix=Nebular()'Helix Nebular'


Helix Nebular

As you can see we can reference the name attribute of the object using the self keyword.

Passing Multiple Arguments in Methods

Methods can receive multiple arguments.

You just separate the arguments with commas.

class Nebular:
    def showName(self,name,nebulartype,distance):

boomerang.showName('Boomerang','Supernova Remnant',243)


Boomerang Supernova Remnant 243

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post