Object Oriented Programming In Python For Beginners part -1

Object Oriented Programming In Python

Object Oriented Programming (OOP) tends to be one of the major obstacles for beginners when they are first starting to learn Python.

This is the second part of object oriented programming for first part click on object oriented programming in python for beginners part -1.

Method

Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of our objects. Methods are a key concept of the OOP paradigm. They are essential to dividing responsibilities in programming, especially in large applications.

You can basically think of methods as functions acting on an Object that takes the Object itself into account through its self argument.

Let's go through an example of creating a Circle class:

class Circle:
    pi = 3.14

    # Circle gets instantiated with a radius (default is 1)
    def __init__(self, radius=1):
        self.radius = radius

Read Also

        self.area = radius * radius * Circle.pi

    # Method for resetting Radius
    def setRadius(self, new_radius):
        self.radius = new_radius
        self.area = new_radius * new_radius * self.pi

    # Method for getting Circumference
    def getCircumference(self):
        return self.radius * self.pi * 2

c = Circle()

print('Radius is: ',c.radius)
print('Area is: ',c.area)
print('Circumference is: ',c.getCircumference())

OUTPUT

Radius is: 1
Area is: 3.14
Circumference is: 6.28

In the __init__ method above, in order to calculate the area attribute, we had to call Circle.pi. This is because the object does not yet have its own .pi attribute, so we call the Class Object Attribute pi instead.
In the setRadius method, however, we'll be working with an existing Circle object that does have its own pi attribute. Here we can use either Circle.pi or self.pi.

Now let's change the radius and see how that affects our Circle object:

c.setRadius(2)

print('Radius is: ',c.radius)
print('Area is: ',c.area)
print('Circumference is: ',c.getCircumference())

OUTPUT

Radius is: 2
Area is: 12.56
Circumference is: 12.56

Great! Notice how we used self. a notation to reference attributes of the class within the method calls. Review how the code above works and try creating your own method.

Inheritance

Inheritance is a way to form new classes using classes that have already been defined. The newly formed classes are called derived classes, the classes that we derive from are called base classes. Important benefits of inheritance are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the functionality of base classes (ancestors).

Let's see an example by incorporating our previous work on the Dog class:

class Animal:
    def __init__(self):
    print("Animal created")

    def whoAmI(self):
        print("Animal")

    def eat(self):
        print("Eating")

class Dog(Animal):
    def __init(self):
        Animal.__init__(self)
        print("Dog created")

    def whoAmI(self):
        print("Dog")

    def bark(self):
        print("Woof!")
d = Dog()

OUTPUT

Animal created
Dog created
d.whoAmI()

OUTPUT

Dog
d.eat()

OUTPUT

Eating
d.bark()

OUTPUT

Woof!


In this example, we have two classes: Animal and Dog. The Animal is the base class, the Dog is the derived class.

The derived class inherits the functionality of the base class.

  • It is shown by the eat() method.
The derived class modifies the existing behavior of the base class.
  • shown by the whoAmI() method.

Finally, the derived class extends the functionality of the base class, by defining a new bark() method.

Post a Comment