-3
我有一個任務要弄清楚下面的代碼是幹什麼的。它看起來像它是在python2中構建的,但我想使用python3。我已經安裝了它需要的argparse並設置了必要的文件路徑,但是每次我在命令行中運行該程序時,都會遇到這些問題。python錯誤NameError:name'ofclass'未定義
Traceback (most recent call last):
File "C:\Users\Name\pythonScripts\Noddy.py", line 6, in <module>
class Noddy:
File "C:\Users\Name\pythonScripts\Noddy.py", line 63, in Noddy
if __name__ == '__main__': main()
File "C:\Users\Name\pythonScripts\Noddy.py", line 57, in main
ent = Noddy.make(fools)
NameError: name 'Noddy' is not defined
代碼如下。
#! python3
class Noddy:
def __init__(self, x):
self.ant = None
self.dec = None
self.holder = x
@classmethod
def make(self, l):
ent = Noddy(l.pop(0))
for x in l:
ent.scrobble(x)
return ent
def scrobble(self, x):
if self.holder > x:
if self.ant is None:
self.ant = Noddy(x)
else:
self.ant.scrobble(x)
else:
if self.dec is None:
self.dec = Noddy(x)
else:
self.dec.scrobble(x)
def bubble(self):
if self.ant:
for x in self.ant.bubble():
yield x
yield self.holder
if self.dec:
for x in self.dec.bubble():
yield x
def bobble(self):
yield self.holder
if self.ant:
for x in self.ant.bobble():
yield x
if self.dec:
for x in self.dec.bobble():
yield x
def main():
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("foo")
args = ap.parse_args()
foo = open(args.foo)
fools = [int(bar) for bar in foo]
ent = Noddy.make(fools)
print(list(ent.bubble()))
print
print(list(ent.bobble()))
if __name__ == '__main__': main()
您是否包含了整個錯誤?我只是看到一個堆棧跟蹤? – OptimusCrime
使用@classmethod和self會導致錯誤。改用cls。 –
有全錯誤更新 –