Python NameError (def) is not defined

maestro777 picture maestro777 · Apr 27, 2017 · Viewed 8k times · Source

I'm having trouble with the following Python code:

class Methods:

    def method1(n):
        #method1 code

    def method2(N):
        #some method2 code
            for number in method1(1):
                #more method2 code

def main():
    m = Methods
    for number in m.method2(4):
            #conditional code goes here

if __name__ == '__main__':
    main()

When I run this code, I get NameError: name 'method1' is not defined. How do I resolve this error?

Answer

Peter Pei Guo picture Peter Pei Guo · Apr 27, 2017

Just add self. in front of it:

self.method1(1)

Also change your method signitures to:

def method1(self, n):

and

def method2(self, n):