I want to print a class attribute, part of a list of instances, only if this attribute exist
i created a class name Guitarist, that includes a name attribute, and otherItems (which usually is a dictionary for the user to add additional data such as band, guitar etc...). It adds the the class to a list (i hope i am not confusing terms) i created another class called FenderGuitarist which inherits Guitarist, and adds an attribute of guitar to the constructor. How do i change the print function, which gets the guitarist list as a parameter, that only if it identifies a guitar attribute, it prints it? note: please ignore the fact that a user can add guitar attribute to the otherItems dicionary as well.
guitarists = []
class Guitarist:
playing_instrument = "guitar"
def __init__(self, name, otherItems=None):
self.name = name
self.otherItems = otherItems
guitarists.append(self)
def __str__(self):
return "Guitarist"
def get_playing_instrument(self):
return self.playing_instrument
def print_list(list):
for item in list:
print(f"name {item.name}", end="")
if item.otherItems is not None:
print(":")
for key,value in item.otherItems.items():
print(key, value)
print()
class FenderGuitarist(Guitarist):
def __init__(self, name, otherItems=None):
Guitarist.__init__(self, name, otherItems=None)
self.guitar = "Fender"
def __str__(self):
return "Fender Guitarist"
print result: name, guitar(if exist) and the otherItems of the dicionary for each item in the list
So for this code i would want the print funcion to identify only tomMisch item as having a guitar attribute and then print it. for the others just print name and everything in the otherItems dicionary:
from classes import *
johnMayer = Guitarist("John Mayer", {"favorite guitar": "Fender/PRS"})
dictJimmy = {"band": "Led Zeppelin", "favorite guitar": "Gibson"}
jimmyPage = Guitarist("Jimmy Page", dictJimmy)
tomMisch = FenderGuitarist("Tom Misch")
print_list(guitarists)