gpt4 book ai didi

python - 属性错误: 'function' object has no attribute 'my_args'

转载 作者:行者123 更新时间:2023-11-30 23:49:47 25 4
gpt4 key购买 nike

如何访问 __main__ 中的 my_args 列表数据类型?

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)

最佳答案

如果您从 one()返回它们,就可以:

#!/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

关于python - 属性错误: 'function' object has no attribute 'my_args' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7523835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com