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

1. Python Object

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

2. 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)

<class ‘int’>

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

<class ‘list’>

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

<function sayhi at 0x064B7300>

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

<class ‘bool’>

>>> type('True')

<class ‘str’>

>>> class fruit:
         pass
>>> fruit

<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.

3. 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()

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()

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

4. 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()

Hi

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

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

True

>>> id(Miley)

95812144

>>> id(Hannah)

95812144

Also, let’s check their hash values.

>>> hash(Miley)

5988259

>>> hash(Hannah)

5988259

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

>>> del Hannah
>>> Miley

<__main__.Person object at 0x05B5FA30>
As you can see, it does not.

5. Assigning Attributes to an Object on the Fly

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

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

Python Interview Questions

6. 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
Traceback (most recent call last):
File "<pyshell#729>", line 1, in <module>
orange

NameError: name ‘orange’ is not defined

7. 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?

Furthermore, if you have any query, feel free to ask in the comment section.

Top python Books to learn Python programming language.

For reference

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.