LU02.L09 - Dataclass mit mutable Attribut

main.py
"""
This script demonstrates how to add a new member to a team.
"""
 
from team import Team
 
def add_member(team, member):
    """
    Adds a new member to the team's members list.
    """
    team.members.append(member)
 
if __name__ == '__main__':
    team1 = Team(name='Team A')
    team2 = Team(name='Team B')
 
    add_member(team1, 'Alice')
    add_member(team2, 'Bob')
 
    print(f'{team1.name} Mitglieder: {team1.members}')  # Output: Team A Mitglieder: ['Alice']
    print(f'{team2.name} Mitglieder: {team2.members}')  # Output: Team B Mitglieder: ['Bob']
team.py
"""
This module contains the Team class.
"""
 
from dataclasses import dataclass, field
 
@dataclass
class Team:
    name: str
    members: list = field(default_factory=list)
  • modul/m323/learningunits/lu02/loesungen/dataclass.txt
  • Zuletzt geändert: 2024/08/28 10:41
  • von kmaurizi