What does the Python error “name 'self' is not defined” mean?

前端 未结 4 1844
心在旅途
心在旅途 2021-01-13 17:32

I can\'t figure out what\'s wrong with this very simple snippet:

class A(object):
        def printme(self):
                print \"A\"

        self.printm         


        
4条回答
  •  天命终不由人
    2021-01-13 17:52

    It's exactly what it says: self is not defined when you call self.printme(). self isn't magically defined for you in Python; it only works inside a method which has an argument named self. If it helps, try replacing the word self with something else, say foo, throughout your program (because there is really nothing special about self as an identifier).

提交回复
热议问题