如何定义全局变量python?

时间:2015-08-29 04:05:35

标签: python

如何在python中使用全局变量?


compare_rects = CompareRectangles(r1,r2)
NameError: global name 'debugging' is not defined

SELECT t.*, CA.someSpecialProperty FROM table1 t CROSS APPLY ( SELECT CASE WHEN x = @mail THEN 1 WHEN y IN ( SELECT y FROM someTable WHERE m = @mail ) THEN 2 WHEN z IN ( SELECT z FROM otherTable WHERE otherM = @mail ) THEN 3 END ) CA (someSpecialProperty) WHERE CA.someSpecialProperty IS NOT NULL ;

2 个答案:

答案 0 :(得分:2)

这不是全球性的,而是一个类变量。您可以self.debugging访问它。我不明白为什么你需要globals()方法。

class CompareRectangles(object):

    debugging = True

    def __init__(self,r1,r2):
        self.r1 = r1
        self.r2 = r2
        self.initialise_boundary_tests()

    def method(self):
        if self.debugging:
            print("hello debugger")

答案 1 :(得分:0)

真的很近。您需要添加self

更改

def method(self):
    if debugging:
        print 'hello debugger'

def method(self):
    if self.debugging:    <-- notice the self
        print 'hello debugger'

Code解释你的例子:)

相关问题