Python Object Tutorial – How to Create, Delete & Initialize Object
Master Python with 70+ Hands-on Projects and Get Job-ready - 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.
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
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
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
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
File “<pyshell#729>”, line 1, in <module>
orange
NameError: name ‘orange’ is not defined
Python Interview Questions on Objects
- What are objects in Python? Explain with example.
- Can you make a list of objects in Python?
- What are the types of objects in Python?
- How to access an object in a list in Python?
- 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?
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google
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.
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.