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.

There are many, many tutorials and lessons covering OOP so feel free to Google search other lessons.

For this lesson we will construct our knowledge of OOP in Python by building on the following topics:

  • Objects
  • Using the class keyword
  • Creating class attributes
  • Creating methods in a class
  • Learning about Inheritance
  • Learning about Polymorphism
  • Learning about Special Methods for classes

Let's start the lesson by remembering the Basic Python Objects. For example:

lst = [1,2,3]

Remember how we could call methods on a list?

print(lst.count(2))

OUTPUT

1

What we will basically be doing in this lecture is exploring how we could create an Object type as a list. We've already learned about how to create functions. So let's explore Objects in general:

Objects

In Python, everything is an object. Remember from previous lectures we can use type() to check the type of object something is:

print(type(1))
print(type([]))
print(type(()))
print(type({}))

OUTPUT

<class 'int'>
<class 'list'>
<class 'tuple'>
<class 'dict'>

So we know all these things are objects, so how can we create our own Object types? That is where the class keyword comes in.

Class

User-defined objects are created using the class keyword. The class is a blueprint that defines the nature of a future object. From classes, we can construct instances. An instance is a specific object created from a particular class. For example, above we created the object lst which was an instance of a list object.

Let see how we can use the class:

# Create a new object type called Sample

Read Also

class Sample:
    pass

#Instance of Sample
x = Sample()

print(type(x))

OUTPUT

<class '__main__.Sample>

By convention, we give classes a name that starts with a capital letter. Note how x is no reference to the out new instance of a Sample class. In other words, we instantiate the sample class.

Inside of the class we currently just have pass. But we can define class attributes and methods.

An attribute is a characteristic of an object. A method is an operation we can perform with the object.

For example, we can create a class called Dog. An attribute of a dog may be its breed or its name, while a method of a dog may be defined by a .bark() method which returns a sound.

Let's get a better understanding of attributes through an example.

Attributes

The syntax for creating an attribute is:
self.attribute = something
There is a special method called:
__init__()

This method is used to initialize the attributes of an objects. For example:
class Dog:
    def __init__(self,breed):
       self.breed = breed

sam = Dog(breed='Lab')
frank = Dog(breed='Huskie')

Let's break down what we have above. The special method


__init__()
is called automatically right after the object has been created:
def __init__(self, breed):
Each attribute in a class definition begins with a reference to the instance object. It is by convetion named self. The breed is the argument. The value is passed during the class instantiation.
self.breed = breed

Post a Comment