Python检测代码中的错误
这段python代码有很多错误,但我想知道是否有人能专门向我解释“没有找到全局回溯”错误和“self不是第一个方法参数”分别出现在第16行和第28行Python检测代码中的错误,python,error-handling,Python,Error Handling,这段python代码有很多错误,但我想知道是否有人能专门向我解释“没有找到全局回溯”错误和“self不是第一个方法参数”分别出现在第16行和第28行 1 #! /usr/bin/env python 2 3 'Example errors caught by PyChecker' 4 5 import string 6 7 metaslash = 1 8 9 def printNames(): 10 neal = 'neal' 11
1 #! /usr/bin/env python
2
3 'Example errors caught by PyChecker'
4
5 import string
6
7 metaslash = 1
8
9 def printNames():
10 neal = 'neal'
11 michelle = 'michele'
12 eric = 5
13 print "Local values: %(neal)S %(michele)s %(eric)" % locals()
14
15 class Nothing:
16 def printValue(value):
17 print value
18 def set(self, value):
19 self.value = value
20
21 def tryToDoSomething(self, value):
22 try:
23 import string
24 if not value:
25 raise RuntimeError, "Hey, there's no value"
26 printNames('a, b, c')
27 except:
28 traceback.print_exc()
29
30 def setGlobal(value=None):
31 print 'Old MetaSlash value is:', metaslash
32 metaslash = value
33 useless = Nothing(5)
34 print 'a useless value is:', useless.valeu
第16行应为:
def printValue(self, value):
在第28行,您调用的回溯对象是什么?Python找不到它。好的,找到它,然后明白你在说什么。那么第16行呢?在第16行,您忘记了
self
,类中的每个函数都需要它(参见答案),而不必这样做。你总是可以用@classmethod
来装饰它来规避这个问题。是的,这是真的,但是初学者pythoneers可能不应该和装饰师混在一起。你能进一步解释一下@classmethod的用法吗?