1
votes

How can i get access to my_args , list data type in main

# 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
you never defined a global variable with that name. And that's not how you access a method - JBernardo

2 Answers

3
votes

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
0
votes

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!