

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create instance Objects using __init__ in Python?
- The instantiation or calling-a-class-object operation creates an empty object. Many classes like to create objects with instances with a specific initial state. Therefore a class may define a special method named __init__(), as follows −
- def __init__(self) −
- self.data = [ ]
- When a class defines an __init__() method, class instantiation automatically invokes the newly-created class instance which is obtained by −
- x = MyClass()
- The __init__() method may have arguments. In such a case, arguments given to the class instantiation operator are passed on to __init__(). For example,
>>> class Complex: ... def __init__(self, realpart, imagpart): ... self.r = realpart ... self.i = imagpart ... >>> x = Complex(4.0, -6.5) >>> x.r, x.i (4.0, -6.5)
- Related Questions & Answers
- Creating Instance Objects in Python
- How to create class objects in Python?
- How to create JavaScript objects using object() constructor?
- How to create JavaScript objects using new operator?
- How does constructor method __init__ work in Python?
- What is __init__.py in Python?
- How to create objects in JavaScript?
- How to create JShell instance programmatically in Java 9?
- How to access Python objects within objects in Python?
- How to create Python objects based on an XML file?
- How to create an instance of an anonymous interface in Kotlin?
- How to create an instance of an abstract class in Kotlin?
- Create class Objects in Java using new operator
- Why is __init__() always called after __new__() in python?
- How to create a directory using Python?
Advertisements