[Python] Python - Creating a many-to-one relation between two classes and a method to...

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Team class and a Driver class.

    class Team:
    def __init__(self, teamName, drivers=None):
    self.teamName = teamName
    if drivers is not None:
    self.drivers = list(drivers)
    else:
    self.drivers = []


    class Driver:
    def __init__(self, name, country, number: str, podiums: int):
    self.name = name
    self.country = country
    self.number = number
    self.podiums = podiums


    The Team class also has a function to add an instance of the Driver class to a particular team. Each team has two drivers and the drivers can switch teams. I can access the drivers that belong to a certain team but how do I access the team that a certain driver belongs to without having to manually add a team parameter in the Driver class?

    Continue reading...

Compartilhe esta Página