Python optional parameters

前端 未结 3 703
别那么骄傲
别那么骄傲 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:31

    Yes; default parameters are evaluated only at the time when the function is defined.

    One possible solution would be to have the parameter be a class rather than an instance, a la

    def foo(blah, klass = B):
        b = klass()
        # etc
    

提交回复
热议问题