" /> python - "Least Astonishment" et le Mutable Default Argument | 2022 codeprofesseur

python - "Least Astonishment" et le Mutable Default Argument

Mots clés : pythonlanguage-designdefault-parametersleast-astonishmentpython

meilleur 4 Réponses python - "Least Astonishment" et le Mutable Default Argument

vote vote

96

fruits = ("apples", "bananas", "loganberries")  def eat(food=fruits):     ... 
def some_random_function():     global fruits     fruits = ("blueberries", "mangos") 
StringBuffer s = new StringBuffer("Hello World!"); Map<StringBuffer,Integer> counts = new HashMap<StringBuffer,Integer>(); counts.put(s, 5); s.append("!!!!"); System.out.println( counts.get(s) );  // does this work? 
vote vote

84

def whats_on_the_telly(penguin=None):     if penguin is None:         penguin = []     penguin.append("property of the zoo")     return penguin 
vote vote

78

a = [] 
def x(a=[]): 
def x(a=datetime.datetime.now()): 
b = datetime.datetime.now() def x(a=b): 
def x(static a=b): 
vote vote

65

class BananaBunch:     bananas = []      def addBanana(self, banana):         self.bananas.append(banana) 

Questions similaires