AttributeError: 'function' object has no attribute 'my_args'
如何访问我的参数,在主目录中列出数据类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 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) |
如果您从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #!/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() ^^^ |
输出:
1 2 3 4 | main function one function i got args from one(): ['QA-65'] QA-65 |
您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/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() |
演示。
仅仅因为你不能,并不意味着你应该!