如何在 __main__ 中创建一个变量,该变量可由同一文件中定义的 web.py 类 使用?

How can I create a variable in __main__ that can be used by web.py classes defined in the same file?

我使用 web.py 和 中的内置网络服务器,而不是 apache,就像在这个问题中一样:Global variable usage with web.py in Apache

我有这个网络服务,它接受一个数字作为参数,将它添加到一个内部变量 number 和 returns 该变量。

脚本如下所示:

import web
import sys

urls = (
    '/(\d+)', 'Number'
)

class Number:
    def GET(self, parameter):
        # I want to read and write to the variable number here

        print "number in web service is " + str(number)

        number = number + int(parameter)
        return number


if __name__ == "__main__":
    #print sys.argv

    # I want to initialise the variable number here, based on command line parameter

    number = int(sys.argv[2]) if len(sys.argv) >= 3 else 0 # initialisation

    print "number init value " + str(number)

    app = web.application(urls, globals())
    app.run()

我这样启动服务器:

sudo python test.py 192.168.0.100 16

其中 16 是内部计数器的初始值。

像这样执行时:

http://192.168.0.100:8080/42

我在这一行 UnboundLocalError: local variable 'number' referenced before assignment

print "number in web service is " + str(number)

好的,所以我需要global,因为我不想让number成为服务的局部变量,我改成:

import web
import sys

urls = (
    '/(\d+)', 'Number'
)

class Number:
    def GET(self, parameter):
        # I want to read and write to the variable number here

        global number # number is not a lcoal variable

        print "number in web service is " + str(number)

        number = number + int(parameter)
        return number


if __name__ == "__main__":
    #print sys.argv

    # I want to initialise the variable number here, based on command line parameter

    number = int(sys.argv[2]) if len(sys.argv) >= 3 else 0 # initialisation

    print "number init value " + str(number)

    app = web.application(urls, globals())
    app.run()

现在我在同一行上调用服务时遇到不同的错误 NameError: global name 'number' is not defined。为了解决这个问题,我在顶部添加了一个初始化:

import web
import sys

urls = (
    '/(\d+)', 'Number'
)

number = 0 # assigning value to define variable

class Number:
    def GET(self, parameter):
        # I want to read and write to the variable number here

        global number # number is not a lcoal variable

        print "number in web service is " + str(number)

        number = number + int(parameter)
        return number


if __name__ == "__main__":
    #print sys.argv

    # I want to initialise the variable number here, based on command line parameter

    number = int(sys.argv[2]) if len(sys.argv) >= 3 else 0 # initialisation

    print "number init value " + str(number)

    app = web.application(urls, globals())
    app.run()

虽然我现在没有收到错误,但逻辑被打破了。虽然初始打印显示 number init value is 16,但另一个打印没有相同的值,它显示 number in web service is 0.

显然,我的代码中有两个单独的 number 变量。我发现 this answer 指出 __main__ 是一个单独的模块。现在解释了 number 的不同值。 但是,当我尝试将global添加到__main__时,像这样:

import web
import sys

urls = (
    '/(\d+)', 'Number'
)

number = 0 # assigning value to define variable

class Number:
    def GET(self, parameter):
        # I want to read and write to the variable number here

        global number # number is not a lcoal variable

        print "number in web service is " + str(number)

        number = number + int(parameter)
        return number


if __name__ == "__main__":
    #print sys.argv

    # I want to initialise the variable number here, based on command line parameter

    global number # I want that one global variable number
    number = int(sys.argv[2]) if len(sys.argv) >= 3 else 0 # initialisation

    print "number init value " + str(number)

    app = web.application(urls, globals())
    app.run()

该值在初始化时再次为 16,在服务上为 0。 我在 __main__

的新行上得到一个 SyntaxWarning: name 'number' is assigned to before global declaration

所以我再次去掉了顶部的赋值,这在一开始并不是真正必要的。因为真正的初始化发生在 __main__:

import web
import sys

urls = (
    '/(\d+)', 'Number'
)

class Number:
    def GET(self, parameter):
        # I want to read and write to the variable number here

        global number # number is not a lcoal variable

        print "number in web service is " + str(number)

        number = number + int(parameter)
        return number


if __name__ == "__main__":
    #print sys.argv

    # I want to initialise the variable number here, based on command line parameter

    global number # I want that one global variable number
    number = int(sys.argv[2]) if len(sys.argv) >= 3 else 0 # initialisation

    print "number init value " + str(number)

    app = web.application(urls, globals())
    app.run()

虽然最初执行时没有警告,但我确实从 GET 方法中得到了 NameError global name 'number' is not defined

如何创建一个可以在 __main__ 中设置的变量,并且我还可以从 class 中定义的方法中读取和写入该变量文件?

问题是因为 auto-re-loader 只是把它放在文件的顶部,一切 应该 工作

import web
import sys



number = int(sys.argv[2]) if len(sys.argv) >= 3 else 0 #initialisation

urls = (
'/(\d+)', 'Number'
)

 ... #the rest of the file

你看,re-loader 会做 "reload(yourmodule)" 所以 __main__ 部分不会被再次调用 :(

干杯