AttributeError: 'function' object has no attribute 'my_args'
How can I get access to my_args
list data type in __main__
?
test1.py开发者_JAVA百科
#!/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)
You can if you return
them from 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() ^^^
Outputs:
main function
one function
i got args from one(): ['QA-65']
QA-65
You can do this using 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.
Just because you can doesn't mean you should!
精彩评论