python - Not getting an output when I call __str__ on a class instance? -
i beginner easy on me. playing __str__
method , found when try print instance 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) key,value in kwargs.items(): setattr(self,key,value) def __str__(self): return "{} {} color {} phone {} facing cameras , {} {}".format(self.__class__.__name__,self.color,self.brand,self.camera,self.cores,self,additional)
from mobile_phone import mobile swiss = mobile() print(swiss) # doesnt show
you have comma need 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) key,value in kwargs.items(): setattr(self,key,value) def __str__(self): return("{} {} color {} phone " "{} facing cameras , {} {}".format( self.__class__.__name__, self.color, self.brand, self.camera, self.cores, self.additional)) # changed self,additional #from mobile_phone import mobile swiss = mobile() print(swiss)
output:
mobile green color reliance phone front-back facing cameras , dual core bluetooth
Comments
Post a Comment