in

Python Object and Classes – Day 13

In this article you will learn python object and classes

In this article you will learn about Python Object and Classes.

Start learning Python from scratch for Free.

Objects are real-world entities. Anything you can describe in this world is an object. Classes on the other hand are not real. They are just a concept. Class is a short form of Classification. A class is a classification of certain objects and it is just a description of the properties and behavior all objects of that classification should possess.

Object-oriented programming stresses on Object.

Objects are real-world entities, we can easily relate objects with real world.

Class in Python

python object and classes

Class can be defined as a blueprint for making objects. just like civil engineers make blueprint before making a house.

What does the class contain?

As a recipe contains a list of ingredients that make up the cake and the directions. Similarly, a class contains the properties/attributes of the object and the operations/behavior that can be performed on an object.

Example:

How to define class in Python?

A class is defined using the class keyword in python.

class Example:

    #This is Python Example class

    pass

Python Built-in Classes

Python has som built in classes which we had used before.

  • List
  • Date
  • Tuple
  • Set

Object in Python

Objects in programming are relatable to the real world. Objects are created to solve real-world problems.

Please Answer

  1. Is sound an Object?
  2. Is light an Object?

Answer

  1. Yes, Sound is an object because it has attributes like Frequency, Wavelength, and behavior like reflection transition.
  2. Yes, light is an object because it has attributes like Pitch, Wavelength, and behavior like reflection transition.

How to create objects in python

To create an object, we need a class. The syntax for creating an object is “<classname>()”, where <classname> is the name of the class.

Example:

Objects of example class

Example()
Example()

Here we created two objects of class Example.

Objects are created but we can not access them because we have not assigned them to any variable.

Example:

mob1=Mobile()
mob2=Mobile()
mob3=Mobile()

Now we can access the objects and reuse them.

Attributes of an object.

This can be done by using the . (dot) operator. The syntax for creating attribute and value for that is as below:

reference_variable.attribute_name=value.

Example:

class Mobile:
    pass
mob1=Mobile()
mob2=Mobile()
mob1.price=20000
mob1.brand="Apple"
mob2.price=3000
mob2.brand="Samsung"

Accessing Attribute value

We can access the attribute values using the dot operator itself. The syntax is as shown below:

reference_variable.attribute_name

For example,

print (mob1.brand)
print (mob2.brand)

 we have to deal with the problem of lack of reuse. For example if an object has 10 attributes and we have 10 objects, then we have to write 100 lines of code! There is no reuse at all.

We need a way to mention the attributes of all the objects of a class in one place so that we can create and initialize the attributes. Let us see how to do this.

How to make attributes in a class?

Attributes can be added to a class through a special function called __init__(). We will discuss more about the syntax later. But for now, this is how the mobile class will look like with attributes in it.When we create an object, the special __init__() function inside the class of that object is invoked automatically. This special function is called as a constructor. Try out the below code and observe the output.

How to add behaviour in a class

We can create behavior in a class by adding functions in a class. However, such functions should have a special parameter called self as the first parameter. We will discuss more about the syntax later on. This is how the purchase behavior in a mobile class would look like:

class Mobile:
    def __init__(self):
        print("Inside constructor")
    def purchase (self):
        print("Purchasing a mobile")
mob1=Mobile()
mob1.purchase()

What is Abstraction in python?

When we invoke the purchase() on a mobile object, we don’t have to know the details of the method to invoke it. We don’t have to know how the reverse() method is working in order to use it in our list.

This ability to use something without having to know the details of how it is working is called abstraction.

Python object and classes

Python object and classes examples

What do you think?

Written by admin

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Loading…

0

Introduction of Python Object-oriented programming(Python Oops) – Day 12

What is On Page SEO? Definitive guide 2020