Why the “mutable default argument fix” syntax is so ugly, asks python newbie

前端 未结 8 662
悲&欢浪女
悲&欢浪女 2020-11-30 09:00

Now following my series of \"python newbie questions\" and based on another question.

Prerogative

Go to http://python.net/~goodger/projects/pyco

8条回答
  •  猫巷女王i
    2020-11-30 09:17

    This is better than good_append(), IMO:

    def ok_append(new_item, a_list=None):
        return a_list.append(new_item) if a_list else [ new_item ]
    

    You could also be extra careful and check that a_list was a list...

提交回复
热议问题