NameError: name 'self' is not defined

前端 未结 3 1916
闹比i
闹比i 2020-11-30 19:52

Why such structure

class A:
    def __init__(self, a):
        self.a = a

    def p(self, b=self.a):
        print b

gives an error

3条回答
  •  臣服心动
    2020-11-30 19:59

    For cases where you also wish to have the option of setting 'b' to None:

    def p(self, **kwargs):
        b = kwargs.get('b', self.a)
        print b
    

提交回复
热议问题