本文介绍了Python3 NameError:未为已定义的@staticmethod定义名称“方法"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python3中编写一个简单的递归函数.在学习OO Java时,我还想编写涉及对象的Python代码.这是我的下面的代码.我提示用户输入一个数字,屏幕应该显示每个小到5的整数.

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)

但是,当我在终端上运行它时,它显示:"NameError:未定义名称'recursive'".错误显示如下:

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

什么原因导致了这里的问题?我知道如何编写递归函数,给它一个参数,然后让它在终端上运行.但是我想练习OOP.

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.

推荐答案

请考虑您已在全局范围内定义了该函数:

consider you have the function defined in the global scope:

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

如果您在类中将其设置为staticmethod,则只需从程序的其他位置并从函数内部使用recusive(10)进行调用:

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

现在它以Recursion.recursive的形式存储在全局范围内,因此这也是您在函数中必须引用它的方式:

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)

但是,如果希望方法直接访问类范围(在函数本地),则可以将其标记为classmethod:

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)

这有几个好处,首先是可以被称为Recursion.recursive(10)x = Recursion() ; x.recursive(),而且如果合适的话,它将使用子类而不是始终使用Recursion:

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

尽管即使您不使用staticmethodclassmethod,您仍然需要引用该方法作为方法:(在Java中,您可以按名称使用这些方法,但是python基本上会强制您使用以下方法: self.METHOD类似于Java的this.METHOD)

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)

希望这可以清除有关方法在python中如何工作的信息!

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

这篇关于Python3 NameError:未为已定义的@staticmethod定义名称“方法"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 01:23