Reputation: 7
I am just a beginner so be easy on me. i was just playing with the __str__
method and found that when I try to print the instance it just doesn't work
import random
brand = ("Samsung","Nokia","Sony","ATAT","Reliance")
no_of_sim = ("Dual-sim","Single-sim")
color = ("Blue","Violet","Orange","Green")
no_of_camera =("Front","Front-Back","Back")
no_of_cores = ("Dual Core","Quad Core","Octa Core")
additional = ("Bluetooth","NFS","Gps")
class mobile:
def __init__(self,**kwargs):
name = self
self.brand = random.choice(brand)
self.sim = random.choice(no_of_sim)
self.color = random.choice(color)
self.camera = random.choice(no_of_camera)
self.cores = random.choice(no_of_cores)
self.additional = random.choice(additional)
for key,value in kwargs.items():
setattr(self,key,value)
def __str__(self):
return "{} Is a {} color {} phone with {} facing cameras and it a {} with {}".format(self.__class__.__name__,self.color,self.brand,self.camera,self.cores,self,additional)
from mobile_phone import mobile
swiss = mobile()
print(swiss)
# It doesnt show up
Upvotes: 0
Views: 72
Reputation: 123463
You have a comma where you need a dot:
import random
brand = ("Samsung","Nokia","Sony","ATAT","Reliance")
no_of_sim = ("Dual-sim","Single-sim")
color = ("Blue","Violet","Orange","Green")
no_of_camera =("Front","Front-Back","Back")
no_of_cores = ("Dual Core","Quad Core","Octa Core")
additional = ("Bluetooth","NFS","Gps")
class mobile:
def __init__(self,**kwargs):
name = self
self.brand = random.choice(brand)
self.sim = random.choice(no_of_sim)
self.color = random.choice(color)
self.camera = random.choice(no_of_camera)
self.cores = random.choice(no_of_cores)
self.additional = random.choice(additional)
for key,value in kwargs.items():
setattr(self,key,value)
def __str__(self):
return("{} Is a {} color {} phone with "
"{} facing cameras and it a {} with {}".format(
self.__class__.__name__,
self.color,
self.brand,
self.camera,
self.cores,
self.additional)) # changed from self,additional
#from mobile_phone import mobile
swiss = mobile()
print(swiss)
Output:
mobile Is a Green color Reliance phone with Front-Back facing cameras and it a Dual Core with Bluetooth
Upvotes: 0
Reputation: 3257
There is a typo in the end of str method:
self,additional
It makes str method recursive. Changing "," to "." removes the problem.
Upvotes: 1