This is a bad convention to call sys.argv from somewhere besides the main function in python

I am writing a script and I have a function, name it f(), which needs one of the command line arguments (the name of the file to open). However, f()it is not called directly in the main function.

I was wondering if the wrong coding was a legend sys.argv[1]directly from f()? If I do not, I will have to pass it as an argument to all the functions that ultimately call f().

+3
source share
7 answers

It would be bad practice to always assume that the arguments your function needs are available on the command line - what if this code was called in some other way?

The function must declare input parameters for the data to which it must access.

At the very least, passing the necessary argument in f()instead of accessing sys.argvhelps make it f()much more reusable.

+6
source

A poor form of access sys.argvfrom anywhere except the main one, since it associates this code with the command line, which makes it difficult to use Python from other scripts.

+4
source

f(). , (, ), sys.argv.

f() ( , "" ), - :

def wrapperf():
    return f(sys.argv[1])

wrapperf main(), , .

+3

, sys.argv f.

f. f main... , ? , ... > . >

, , .: P

+1

, sys.argv [1], , , . . , , .

+1

sys.argv . , , main(), f().

0

I would generally advise him, but BaseHttpServer in python actually uses sys.argv [1] to determine which port to listen on. Convenience may be.

0
source

Source: https://habr.com/ru/post/1757009/


All Articles