[Python] How can I return a list nested in a class [closed]

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    Currently I'm practicing OOP by trying to code a music store backend, here's the snippet I'm having issues with:

    class Guitar:
    guitar_list = []
    def __init__(self, brand, model):
    self.brand = brand
    self.model = model
    Guitar.guitar_list.append(self)
    def printGuitar(self):
    return ("{}, {}".format(self.brand, self.model))

    guitar1 = Guitar('Gibson', 'Les Paul')
    guitar2 = Guitar('Fender', 'Stratocaster')


    Ideally, I would like to access the values stored in this list from outside the class.

    This module will then be imported from another module and I need a method I can call that will return:

    ['Gibson, Les Paul' 'Fender, Stratocaster']


    I would then pass this to my frontend through Flask to display it on a website.

    I have tried using the __repr__ method and defining a custom method, however I've been unable to accomplish this.

    How could I achieve this?

    Continue reading...

Compartilhe esta Página