What does the following code output? from dataclasses import dataclass, field @dataclass class Config: tags: list = field(default_factory=list) c1 = Config() c2 = Config() c1.tags.append(‘x’) print(c2.tags)

Python Professional Hard

Python Professional — Hard

What does the following code output? from dataclasses import dataclass, field @dataclass class Config: tags: list = field(default_factory=list) c1 = Config() c2 = Config() c1.tags.append(‘x’) print(c2.tags)

Key points

  • Instances of Config have independent tag lists
  • Modifying one instance's tag list doesn't affect others
  • default_factory=list creates a new list for each instance

Ready to go further?

Related questions