Special welcome gift. Get 50% off your first courses with code “AS50”. Find out more!

HomeInterviewIn Python, how do you overload methods or constructors?
category box 5

In Python, how do you overload methods or constructors?

The constructor of Python: _ A class’s init__ () method is the first one. Python automatically calls __init__() to initialize object members whenever we attempt to instantiate an object. In Python, we can’t overload constructors or methods. If we attempt to overload, it displays an error.

Example:

  1. class student:    
  2.     def __init__(self, name):    
  3.         self.name = name    
  4.     def __init__(self, name, email):    
  5.         self.name = name    
  6.         self.email = email    
  7.          
  8. # This line will generate an error    
  9. #st = student(“rahul”)    
  10.     
  11. # This line will call the second constructor    
  12. st = student(“rahul”, “rahul@gmail.com”)    
  13. print(“Name: “, st.name)  
  14. print(“Email id: “, st.email)  

Output:

Name: rahul Email id: rahul@gmail.com

Share:

Leave A Reply

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

Categories

ads sidebar 1

You May Also Like

Oracle has several modes for shutting down the database: In normal mode, the database is shut down by default. It...
Materialized views are items that contain condensed sets of data from base tables that have been summarized, clustered, or aggregated. They...
Every database in Oracle has a tablespace called SYSTEM, which is generated automatically when the database is created. It also...