Python3 NameError: name 'method' is not defined for defined @staticmethod
Python3 NameError: name 'method' is not defined for defined @staticmethod
我正在尝试在 python3 中编写一个简单的递归函数。由于我正在学习 OO Java,所以我也想编写 Python 涉及对象的代码。下面是我的代码。我提示用户输入一个数字,屏幕应显示小于 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: name 'recursive' is not defined"。错误如下所示:
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。
考虑到您在全局范围内定义了函数:
def recursive(x):
if (x>5):
print (x)
recursive(x - 1)
你可以简单地从程序的其他地方用 recusive(10)
调用它,同样地从函数内部调用它,如果你在 class:[=27= 中将它设为 staticmethod
]
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
存储在全局范围内,因此这也是您在函数中引用它的方式:
class Recursion:
@staticmethod
def recursive(x):
if (x>5):
print (x)
Recursion.recursive(x - 1)
但是,如果您希望方法可以直接访问 class 作用域(在函数的本地),您可以将其标记为 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()
,而且如果合适它会使用 subclass 而不是总是使用 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
尽管即使您不使用 staticmethod
或 classmethod
您仍然需要引用该方法,作为方法:(在 java 中您可以通过名称,但 python 基本上强制您像 self.METHOD
一样使用方法,类似于 java 的 this.METHOD
)
class Recursion:
def recursive(self,x):
if (x>5):
print (x)
self.recursive(x - 1)
希望这能澄清 python 中方法的工作原理!
我正在尝试在 python3 中编写一个简单的递归函数。由于我正在学习 OO Java,所以我也想编写 Python 涉及对象的代码。下面是我的代码。我提示用户输入一个数字,屏幕应显示小于 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: name 'recursive' is not defined"。错误如下所示:
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。
考虑到您在全局范围内定义了函数:
def recursive(x):
if (x>5):
print (x)
recursive(x - 1)
你可以简单地从程序的其他地方用 recusive(10)
调用它,同样地从函数内部调用它,如果你在 class:[=27= 中将它设为 staticmethod
]
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
存储在全局范围内,因此这也是您在函数中引用它的方式:
class Recursion:
@staticmethod
def recursive(x):
if (x>5):
print (x)
Recursion.recursive(x - 1)
但是,如果您希望方法可以直接访问 class 作用域(在函数的本地),您可以将其标记为 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()
,而且如果合适它会使用 subclass 而不是总是使用 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
尽管即使您不使用 staticmethod
或 classmethod
您仍然需要引用该方法,作为方法:(在 java 中您可以通过名称,但 python 基本上强制您像 self.METHOD
一样使用方法,类似于 java 的 this.METHOD
)
class Recursion:
def recursive(self,x):
if (x>5):
print (x)
self.recursive(x - 1)
希望这能澄清 python 中方法的工作原理!