Musical instruments

Report a typo

Suppose we have a class MusicalInstrument. It has one class attribute, n_instruments, and two instance attributes, corresponding to the name of the musical instrument and its group. We created four instances of this class.

class MusicalInstrument:
    n_instruments = 0  # number of instruments

    def __init__(self, name, group):
        self.name = name
        self.group = group


drums = MusicalInstrument("drums", "percussion")
triangle = MusicalInstrument("triangle", "percussion")
violin = MusicalInstrument("violin", "string instr.")
flute = MusicalInstrument("flute", "woodwind instr.")

Now, we want to change the attribute n_instruments via the instances and print the new result.

drums.n_instruments += 1
triangle.n_instruments += 1
violin.n_instruments += 1
flute.n_instruments += 1

print(MusicalInstrument.n_instruments)

What will be the output of the program?

Enter a number
___

Create a free account to access the full topic