Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

python - Python3 NameError: name 'method' is not defined for defined @staticmethod

I am trying to write a simple recursive function in python3. As I am learning OO Java, I also want to write Python code involving objects. Here's my code below. I prompt the user to enter a number and the screen should display every integer smaller until 5.

class Recursion:
    @staticmethod
    def recursive(x):
        if (x>5):
            print (x)
            recursive(x - 1)

def main(self):
    x = int(input('Enter a number for recursive addition: '))
    recursive(x)

However, when I run it on a terminal, it says: "NameError: name 'recursive' is not defined". Here's what the error looks like:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Recursion import *
>>> a = Recursion()
>>> a.main()
Enter a number for recursive addition: 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ZycinG/Desktop/Python Practice/Recursion.py", line 9, in main
    recursive(x)
NameError: name 'recursive' is not defined

What causes the problem here? I know how to just write the recursive function, give it an argument, and let it run on the terminal. But I want to practice OOP.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

consider you have the function defined in the global scope:

def recursive(x):
    if (x>5):
        print (x)
        recursive(x - 1)

you would simply call this with recusive(10) from elsewhere in the program and similarly from within the function, if you make it a staticmethod within a class:

class Recursion:
    @staticmethod
    def recursive(x):
        if (x>5):
            print (x)
            recursive(x - 1) #this isn't how you call it any more

now it is stored in the global scope as Recursion.recursive so that is also how you would have to refer to it within the function:

class Recursion:
    @staticmethod
    def recursive(x):
        if (x>5):
            print (x)
            Recursion.recursive(x - 1)

However if you want a method to have access to the class scope directly (locally to the function) you can label it a classmethod:

class Recursion:
    @classmethod
    def recursive(cls,x): #the first argument is the class
        if (x>5):
            print (x)
            cls.recursive(x - 1)

this has several benefits, first that it can be called as Recursion.recursive(10) or x = Recursion() ; x.recursive() but also that it will use a subclass if appropriate instead of always using Recursion:

class Recursion:
    def __init__(self,x=None):
        raise NotImplementedError("not intended to initialize the super class")
    @classmethod
    def recursive(x):
        if (x>5):
            print (x)
            cls.recursive(x - 1)
        else:
            return cls(x)

class R_sub(Recursion):
    def __init__(self,x):
        self._val = x
#now using R_sub.recursive(10) will work fine

although even if you do not use staticmethod or classmethod you still need to refer to the method, as a method: (in java you can use the methods just by name but python basically forces you to use methods as self.METHOD similarly to java's this.METHOD)

class Recursion:
    def recursive(self,x):
        if (x>5):
            print (x)
            self.recursive(x - 1)

Hope this clears things up about how methods work in python!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...