访问另一个函数中在函数中声明的变量

时间:2019-02-14 00:17:58

标签: python

如何从另一个在x内部调用的函数main()中访问函数sub()本地的变量main()

运行脚本时,下面的MEW引发错误NameError: global name 'x' is not defined

如果我在global x, l中添加main(),就不会再有错误了,但是我不想这样使这些变量成为全局变量。

MWE:

def sub():
    print "x from main: {}".format(x)
    print "l from main: {}".format(l)


def main():
    x = 5
    l = [1,2,3,4,5]
    print "z from top: {}".format(z)
    sub()


if __name__ == "__main__":
    z = 'SOME'
    main()

Edit1 我正在编辑现有代码,但不希望更改功能范围

2 个答案:

答案 0 :(得分:3)

将参数作为参数传递即可实现所需的功能。

def sub(x, l):
    print "x from main: {}".format(x)
    print "l from main: {}".format(l)


def main(z):
    x = 5
    l = [1,2,3,4,5]
    print "z from top: {}".format(z)
    sub(x, l)


if __name__ == "__main__":
    z = 'SOME'
    main(z)

此打印:

z from top: SOME
x from main: 5
l from main: [1, 2, 3, 4, 5]

如果您不想使用这种方法,那么我猜您可以采用this答案的方法,尽管在这种情况下似乎有些困惑。

import inspect

z = 'SOME'

def sub():
    frame = inspect.currentframe()
    locales = None
    try: locales = frame.f_back.f_locals
    finally: del frame
    if locales:
        for k, v in locales.items():
            print("{} from main: {}".format(k, v))


def main():
    x = 5
    l = [1,2,3,4,5]
    print("z from top: {}".format(z))
    sub()

main(z)

如果您可以将其转变为上下文管理器,也许这样会更清洁。

您还可以使用*args*kwargs

def function(*args, **kwargs):
    if args:
        for a in args:
            print "value: {} was passed in args".format(a)
    if kwargs:
        for k, v in kwargs.items():
            print "key {} was passed with value: {} in kwargs".format(k, v)


def caller():
    x = 5
    l = [1,2,3,4,5]
    function(x, l = l)

caller()
#value: 5 was passed in args
#key l was passed with value: [1, 2, 3, 4, 5] in kwargs

您可以在此处为locals分配值

function

之内
locals['x'] = args[0]
locals['l'] = kwargs[l]

使用dict.get可以设置默认值:

l = kwargs.get('l', None)

答案 1 :(得分:1)

做您想做的最好的方法,也需要最少的更改。只需更改声明变量的方式即可;通过在变量名之前添加函数名,您可以从代码中的任何位置访问变量。

def sub():
    print "x from main: {}".format(main.x)
    print "l from main: {}".format(main.l)

def main():
    main.x = 5
    main.l = [1,2,3,4,5]
    print "z from top: {}".format(z)
    sub()

if __name__ == "__main__":
    z = 'SOME'
    main()

这将打印:

z from top: SOME
x from main: 5
l from main: [1, 2, 3, 4, 5]