2

Here's an example on what I'm thinking:

def check_args():
    if len(sys.argv) < 2:
        sys.exit('Reason')
    if not os.path.exists(sys.argv[1]):
        sys.exit('ERROR: Folder %s was not found!' % sys.argv[1])
    global path
    path = sys.argv[1]

As you can tell after check_args() completes succefully the "path" variable has universal meaning - you can use it in other functions as de-facto variable.

So I'm thinking if there is a way to access "path" e.g. check_args.path?

How about in python v3?

4

2 に答える 2

3

Python functions are objects. You can define whatever custom properties you want on them:

def a():
    a.test = 123

a()
a.test # => 123
于 2012-06-15T19:16:19.447 に答える
0

You can simply return the value

def check_args():
    if len(sys.argv) < 2:
        sys.exit('Reason')
    if not os.path.exists(sys.argv[1]):
        sys.exit('ERROR: Folder %s was not found!' % sys.argv[1])
    path = sys.argv[1]
    return path

my_path = check_args()

or when using if __name__ ...:

if __name__ == '__main__':
    my_path = check_args()
    results = do_something(path)
于 2012-06-15T20:41:13.080 に答える