Python optional parameters

前端 未结 3 708
别那么骄傲
别那么骄傲 2020-12-29 22:53

Guys, I just started python recently and get confused with the optional parameters, say I have the program like this:

class B:
   pass

class A:
    def __in         


        
3条回答
  •  余生分开走
    2020-12-29 23:41

    you need to do the following:

    class A:
        def __init__(self, builds=None):
            if builds is None:
                builds = B()
            self.builds = builds
    

    it's a very wide-spread error, using mutable parameters as a default arguments. there are plenty of dupes probably on SO.

提交回复
热议问题