NameError: name 'self' is not defined

前端 未结 3 1917
闹比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 20:15

    If you have arrived here via google, please make sure to check that you have given self as the first parameter to a class function. Especially if you try to reference values for that object instance inside the class function.

    def foo():
        print(self.bar)
    

    >NameError: name 'self' is not defined

    def foo(self):
        print(self.bar)
    

提交回复
热议问题