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

时间:2016-01-11 18:36:53

标签: python scope web.py

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

我有这个Web服务,它接受一个数字作为参数,将它添加到内部变量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

        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。 我在SyntaxWarning: name 'number' is assigned to before global declaration

中的新行上获得__main__

所以我再次摆脱了顶部的任务,这首先不是必需的。因为真正的初始化发生在__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()

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

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

1 个答案:

答案 0 :(得分:0)

问题是因为自动重新加载器只是把它放在文件的顶部而且应该的所有内容

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

你知道,重新加载器将会重新加载(你的模块)"所以__main__部分不会被再次调用:(

干杯