AttributeError:'function'对象没有属性'my_args'

时间:2011-09-23 03:06:49

标签: python

如何访问我的args,列出 man

中的数据类型
# test1.py 

#!/usr/bin/env python

def main():
    print 'main function'
    one()

def one():
    print 'one function'
    my_args = ["QA-65"]

def two():
    print 'two function'

if __name__ == "__main__":
    main()
    getattr(my_args, pop)

2 个答案:

答案 0 :(得分:3)

如果return来自one(),您可以{/ 1}}。

#!/usr/bin/env python

def main():
    print 'main function'
    args = one() # returns my_args, which i'm assigning to args
    print 'i got args from one():', args
    print args.pop()

def one():
    print 'one function'
    my_args = ["QA-65"]
    return my_args

def two():
    print 'two function'

if __name__ == "__main__":
    main()
    #getattr(my_args, pop)
    # ^^^ Moved this up to main() ^^^

输出:

main function
one function
i got args from one(): ['QA-65']
QA-65

答案 1 :(得分:0)

可以使用global执行此操作。

#!/usr/bin/env python

def main():
    print 'main function'
    one()

def one():
    print 'one function'
    global my_args
    my_args = ["QA-65"]

def two():
    print 'two function'

if __name__ == "__main__":
    main()
    print my_args.pop()

Demo

仅仅因为你 可以 并不意味着你应该这样做!