Python Programming Using Problem Solving by H. Bhasin
Author:H. Bhasin [Oswald Campesato]
Language: eng
Format: epub
ISBN: 9781683928621
Publisher: David Pallai
Published: 2023-07-15T00:00:00+00:00
============ RUN C:/Python/Class/Constructor2.py ===============
Name :Naved
Age :32
>>>
12.7 MULTIPLE __INIT__(s)
Having the same name function in a class, with different number of parameters, or different types of parameters, is called function overloading. In C++, JAVA, C#, etc., the constructors can also be overloaded; one can have more than one constructor, each having different parameters. In Python, however, we cannot have more than one __init__ in a class. For example, an error crops up if we try executing the following code.
Note, that if one makes a parameterized __init__, Python looks for the rest of the parameters in the instantiation.
Code:
class employee:
âââdef getdata(self):
ââââââself.name=input('Enter name :')
ââââââself.age=input('Enter age :')
âââdef putdata(self):
ââââââprint('Name :',self.name)
ââââââprint('Age :',self.age)
âââdef __init__(self, name, age):
ââââââself.name=name
self.age=age
e1= employee()
e1.getdata()
e1.putdata()
e2=employee('Naved', 32)
e2.putdata()
>>>
============== RUN C:/Python/Class/Constructor2.py =============
Traceback (most recent call last):
ââ File "C:/Python/Class/Constructor2.py", line 11, in <module>
ââââe1=employee()
TypeError: __init__() missing 2 required positional arguments: 'name' and 'age'
>>>
Having studied the importance and implementation of constructors, let us now implement a constructor; let us revisit the âmovieâ class, created above. The following code has a movie class, which contains a getdata() and putdata() function and __init__(self) for initializing the variables. Note that the object âmâ does not call the getdata() function but just putdata(). The values assigned in the constructor are displayed.
Code:
class movie:
âdef getdata(self):
ââ self.name=input('Enter name :')
ââ self.year=int(input('Enter year :'))
ââ self.genre=input('Enter genre :')
ââ self.director=input('Enter the name of the director :')
ââ self.producer=input('Enter the producer :')
âââ L=[]
âââ item=input('Enter the name of the actor :')
ââ L.append(item)
âââ choice=input('Press 'y' for more 'n' to quit')
ââ while(choice == "y"):
âââââââââitem=input('Enter the name of the actor :')
âââââ L.append(item)
âââââââââchoice=input('Enter 'y' for more 'n' to quit')
self.actors=L
self.music_director=input('Enter the name of the music director :')
âââdef putdata(self):
ââââââprint('Name :',self.name)
ââââââprint('Year ',self.year)
ââââââprint('Genre :',self.genre)
ââââââprint('Director :',self.director)
ââââââprint('Producer :',self.producer)
ââââââprint('Music_director :',self.music_director)
ââââââprint('Actors :',self.actors)
âââdef __init__(self):
ââââââself.name='Fault'
ââââââself.year=2015
ââââââself.genre='Drama'
ââââââself.director='XYZ'
ââââââself.producer='ABC'
ââââââself.music_director='LMN'
ââââââself.actors=['A1', 'A2', 'A3', 'A4']
m=movie()
#m.getdata()
m.putdata()
============= RUN C:\Python\Class\class_basic2.py ==============
Nameââââ: Fault
Yearâ âââ2015
Genreââââ: Drama
Directorââ: XYZ
Producerââ: ABC
Music_directorâ:LMN
Actorsâ:['A1', 'A2', 'A3', 'A4']
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26522)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19007)
The Fifty Shades Trilogy & Grey by E L James(18959)
Shot Through the Heart by Mercy Celeste(18880)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16983)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16875)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16804)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16698)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16325)
The Subtle Art of Not Giving a F*ck by Mark Manson(14261)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14072)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13420)
Scorched Earth by Nick Kyme(12715)
Drei Generationen auf dem Jakobsweg by Stein Pia(10922)
Suna by Ziefle Pia(10846)
Scythe by Neal Shusterman(10271)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9476)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9316)
This is Going to Hurt by Adam Kay(9098)
