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:
- It must begin with a letter or underscore.
- It can be comprised only of
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: pass orion=Nebular() print(orion)
<__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)
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.')
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:
- They start with
- They are followed by method identifier or name.
- Then a set of parenthesis containing parameter list.
- They are terminated by a semicolon.
- Then the statements to constitute the body of the method are indented.
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
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()
As you can see we can reference the
name attribute of the object using the
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)
Boomerang Supernova Remnant 243