AppDividend
Latest Code Tutorials

Python Classes And Objects Example | Object In Python

0

Python Classes And Objects Example | Object In Python is today’s topic. A python is an object-oriented programming language. Unlike procedure-oriented programming, where the main emphasis is on functions, object-oriented programming stress on objects. An object is simply a collection of data (variables) and methods (functions) that act on those data. And, the class is a blueprint for the object.

Python Classes And Objects

By the end of this demo tutorial, you will be able to:

  1. Define what the class is.
  2. Define a class in Python.
  3. Attributes and Methods in Python.
  4. Creating an object in Python.
  5. Accessing Object Variables
  6. Constructors in Python.

#What is a class in Python

A class is the code template for creating the objects.

Objects have member variables and methods.

In python, the class is created by a keyword class.

We can think of the class as the sketch (prototype) or blueprint of the house. It contains all the details about the doors, windows, floors, etc. Based on these descriptions, we can build the house. House is the object.

#Defining a Class in Python

We define a class using the keyword class.

The first string is called docstring and has a brief description about the class. Although not mandatory, this is recommended.

See the following syntax.

class AppClass:
    '''This is a docstring'''
    pass

A class creates the new local namespace where all its attributes are defined. Attributes may be the data or functions.

As soon as we define the class, the new class object is created with the same name. This class object allows us to access a different attributes as well as to instantiate the new objects of that class.

The simplest class can be created using a class keyword. For example, let’s create a simple, empty class with no functionalities.

# app.py

class Netflix:
    pass


shows = Netflix()
print(shows)

See the following output.

➜  pyt python3 app.py
<__main__.Netflix object at 0x103448320>
➜  pyt

Let’s define the function and properties inside the class and access the function and properties.

# app.py

class StrangerThings:
    eleven = 11

    def oneLiner(self):
        print('Friends Don\'t Lie')


print(StrangerThings.eleven)

print(StrangerThings.oneLiner)

See the following output.

➜  pyt python3 app.py
11
<function StrangerThings.oneLiner at 0x103440ae8>
➜  pyt

#Attributes and Methods in the class

In the above code, we have defined attributes and methods.

A class by itself is of no use unless there is some functionality associated with it.

Functionalities are defined by setting attributes, which act as containers for data and functions related to those attributes. Those functions are called methods.

Let’s define one attribute of the class.

# app.py

class StrangerThings:
    age = 11


print(StrangerThings.age)

In the above code, the age is an attribute of the class whose value is 11. So, it will output 11. Now, let’s define the method.

# app.py

class StrangerThings:
    item = 'Eggos'

    def food(self):
      print(self.item)

print(StrangerThings.food)

In the above code example, we have defined one method called food and also defined one attribute called item whose value is Eggos.

If we want the return value of the function, then we have to define one object and call the method on that object to get the value return from that function.

#Creating an Object in Python

We saw that a class object could be used to access many different attributes.

It can also be used to create the new object instances (instantiation) of that class. The procedure to create the object is similar to the function call.

See the following syntax of how we can create an instance.

object = Class()

The above code will create the new instance object named object. We can access the attributes of objects using the object name prefix.

Attributes can be data or method. Method of the object is corresponding functions of that class. Any function object that is the class attribute defines the method for objects of that class.

This means to say since Class.func is a function object (attribute of class), object.func will be a method object. Now, see the following code example.

# app.py

class StrangerThings:
    item = 'Eggos'

    def food(self):
        print(self.item)


st = StrangerThings()
st.food()

See the following output.

➜  pyt python3 app.py
Eggos
➜  pyt

You may have noticed a self parameter in function definition inside the class, but, we called the method directly as st.food() without any arguments. It still worked.

It is because, whenever the object calls its method, the object itself is passed as the first argument. So, st.food() translates into StrangerThings.food.

#Accessing Object Variables

We can access the variable using the newly created object using the following code.

# app.py

class StrangerThings:
    def __init__(self, name, age):
        self.name = name
        self.age = age

st = StrangerThings("Eleven", 11)
mbb = StrangerThings("Millie", 15)

print(st.name)
print(st.age)
print(mbb.name)
print(mbb.age)

See the following output.

➜  pyt python3 app.py
Eleven
11
Millie
15
➜  pyt

#Constructors in Python

The class functions that begin with the double underscore (__) are called special functions as they have special meaning.

Of one particular interest is an __init__() function. This special function gets called whenever the new object of that class is instantiated.

This type of function is also called the constructors in Object-Oriented Programming (OOP). We usually use it to initialize all the variables.

Consider the following example to initialize the StrangerThings class attributes.

# app.py

class StrangerThings:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display(self):
        print("Age: %d \nName: %s" % (self.age, self.name))


st = StrangerThings("Eleven", 11)
mbb = StrangerThings("Millie", 15)

st.display()
mbb.display()

See the following output.

➜  pyt python3 app.py
Age: 11
Name: Eleven
Age: 15
Name: Millie
➜  pyt

Finally, Python Classes And Objects Example | Object In Python is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.