The ‘Shared List’ Trap in Python Class Attributes: Why Your Friend Became Everyone’s Friend?

The 'Shared List' Trap in Python Class Attributes: Why Your Friend Became Everyone's Friend?

Today, I want to share a very common pitfall in Python—strange behavior when the default value of a class attribute is a mutable object (like a list). A seemingly simple example Let’s first look at a simple <span>Person</span> class definition: class Person: def __init__(self, name:str, frients:list=[]): self.name = name self.frients = frients def show_frients(self): print(f"{self.name}, … Read more

C++ Default Parameters vs Function Overloading: How to Choose?

C++ Default Parameters vs Function Overloading: How to Choose?

Imagine you are ordering a cup of milk tea đź§‹… “Boss, I want a cup of pearl milk tea!” “Do you want to adjust the sweetness and ice level?” “No, the default is fine!” This scene seems very familiar, right? In the programming world, C++ default parameters are like the “standard configuration” at a milk … Read more