Object Oriented Programming


Python Classes and Objects


Python is one among many object-oriented programming languages.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.



Creating a Class


Keyword class is used to create a class.


Example

Class creation example

Create Object

When a class is created, it bundles in all the features and methods. Thus, we can use the class to create objects.


Example

Object creation example

Output

200


__init__() Function

__init__() function is a built-in function used for initialization, i.e., assigning values to properties or other operations necessary when the object is being created.


Example

__init__ function example

Output

37

Smith


__str__() Function

__str__() function is used to control what should be returned as a string when the object is printed.

Example 1

__str__ function example

Output

<__main__.MakeLabs object at 0x0000019883245F10>


Example 2

__str__ function example

Output

Smith-37



Object Methods


Functions that belong to objects are called methods. Let's create a function in the MakeLabs class.

Example

Object method example

Output

My name is Bond, James Bond



The self Parameter


The self parameter references the current instance of the class. The self parameter is used to access variables that belong to the class.

The name self is a convention to be followed.