Python Object Tutorial – How to Create, Delete & Initialize Object

Free Python courses with 57 real-time projects - Learn Python

Honestly, anything in python programming Language is an object. In this python object tutorial, we will focus on what is Python object, instance Python object, and initialization.

Along with this, we will cover how to create python object, and how to delete an object in python with examples and syntax.

So, let’s Python Object Tutorial.

Python Object - introduction

Python Object Tutorial – How to Create, Dthe elete & Initialize Object

What is Python Object?

To prove our point that everything in Python is an object, we’ll first take some  example. Before this, let’s revise Python Syntax.

>>> type(7)

Output

<class ‘int’>

>>> a=[1,2,3]
>>> type(a)

Output

<class ‘list’>

>>> def sayhi():
  print("Hello")
>>> sayhi

Output

<function sayhi at 0x064B7300>

>>> a=True
>>> type(a)

Output

<class ‘bool’>

>>> type('True')

Output

<class ‘str’>

>>> class fruit:
         pass
>>> fruit

Output

<class ‘__main__.fruit’>

See the point? This is because every class is derived from the class ‘object’. Now, let’s talk about python instance objects.

An object is any real-world entity that has attributes and behaviors. Honestly, an object is to a class as is a prototype to a blueprint.

Python Object Initialization

When we create object for a class, the __init__() method is called.

>>> class fruit:            
       def __init__(self,color,shape):      
               self.color=color                 
               self.shape=shape             
       def sayhi(self):         
               print(f"Hi.\nI am {self.color}and{self.shape}")       
>>> orange=fruit('Orange','Round')
>>> orange.sayhi()

Output

Hi.I am Orange and Round

We use this to fill in values to attributes when we create a object. Here, __init__() has two attributes apart from ‘self’- color and shape.

Then, we pass corresponding arguments for these at the time of object creation.

You know that we use ‘self’ to be able to refer to the object we’re dealing with. Here, that is ‘orange’.

We’ve discussed this in our comprehension on Python Methods.

However, if you don’t supply the __init__() method, Python will use a default one for you.

>>> class Person:            
       def sayhi(self):  
              print("Hi")                     
>>> Hannah=Person()
>>> Hannah.sayhi()

Output

Hi

In saying this, we conclude that __init__() is like a constructor in C++.

Assigning One Object to Another Python Object

Like we do with any other construct in Python, it is possible to assign one object to another using the assignment operator in python.

>>> Miley=Hannah
>>> Miley.sayhi()

Output

Hi

Are these the same? Let’s check with the id() function.

>>> id(Miley)==id(Hannah)

Output

True

>>> id(Miley)

Output

95812144

>>> id(Hannah)

Output

95812144

Also, let’s check their hash values.

>>> hash(Miley)

Output

5988259

>>> hash(Hannah)

Output

5988259

We assigned Hannah to Miley. Now, what if we delete Hannah? How does it affect Miley?

>>> del Hannah
>>> Miley

Output

<__main__.Person object at 0x05B5FA30>

As you can see, it does not.

Assigning Attributes to an Object on the Fly in Python

So what, if only one object out of all, for a class needs an attribute called ‘size’?

Simply assign it on the fly even after you have already declared the python class.

>>> orange.size=7
>>> orange.size

Output

7

This attribute belongs to ‘orange’, but not to ‘fruit’. But it is possible to assign an attribute to ‘fruit’ instead.

How to Delete Python Object?

Garbage collection is a very important aspect of any good programming language. Likewise, Python lets us delete a lot of stuff, including objects.

To delete an object in Python, we use the ‘del’ keyword. A when we try to refer to a deleted object, it raises NameError.

>>> del orange
>>> orange

Output

Traceback (most recent call last):
File “<pyshell#729>”, line 1, in <module>
orange
NameError: name ‘orange’ is not defined

Python Interview Questions on Objects

  1. What are objects in Python? Explain with example.
  2. Can you make a list of objects in Python?
  3. What are the types of objects in Python?
  4. How to access an object in a list in Python?
  5. How to convert an object to a list in Python?

Conclusion

Hence, we have covered the three basic concepts of python object-oriented programming, object initialization in python, assigning one object to another object in python, assigning attributes to an object on the fly, deleting a python object.

These are python classes, python methods, and python objects.

Classes and objects in Python are very important part of Python Programming Language. Indeed, what is a class without its objects?

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

2 Responses

  1. Kusuma says:

    I which way we have to learn python? In the middle of the blog you are mentioning some of the other topics but while referring those concepts they are further topics in the menu given behind. So please suggest me how to learn.

    • DataFlair Team says:

      Hello Kusuma,

      You can refer our sidebar to learn Python. In our sidebar, we have provided the sequence of Python tutorials in which you should learn.

Leave a Reply

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