Python Object Oriented Programming - Class,Attributes and Methods


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:
    pass

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.

instance1=HelloWorld()

Then we can even print that instance:

class Nebular:
    pass

orion=Nebular()
print(orion)

Result

<__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:
    pass
# instantitae Nebular and assign attributes
orion=Nebular()
orion.fullName='Orion Nebular'
orion.category='HII Region'

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

# Print out attributes
print(orion.fullName,orion.category)
print(bernad68.fullName,bernad68.category)

Result

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):
        print(description)

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

Result

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):
        print(self.name)

helix=Nebular()
helix.name='Helix Nebular'
helix.showName()

Result

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):
        print(name,nebulartype,distance)

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

Result

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. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post