Terminology: Argv, Invoking a program

最后都变了- 提交于 2019-12-11 11:56:58

问题


Hy Python Community -

This is a basic terminology question about Argv and "invoke"

I'm new to python and programmring.

I was reading about the argv function in the sys module on openbookproject.com:

"The argv variable holds a list of strings read in from the command line when a Python script is run. These command line arguments can be used to pass information into a program at the same time it is invoked." http://openbookproject.net/thinkcs/python/english2e/ch10.html

It seems really clear from the defintion, but I still wanted to double check: Does "at the time it is invoked" just mean, "when you run the program?" Would it be appropriate in a third way to say, "Argv can pass information into a program at runtime?"

Thank you.


回答1:


  1. Yes, that's what "invoked" means.

  2. No, because "at runtime" covers the entire time window in which the process is running. It is precisely accurate to say that argv can pass information into a program at invocation.




回答2:


Does "at the time it is invoked" just mean, "when you run the program?"

Yes. "at the same time it is invoked" implies that you can pass data to the program later while it is running too i.e., you can use command-line arguments (sys.argv) to pass data to the program "at the same time it is invoked" and some other means (IPC) to pass it later e.g., via standard input while it is running.

Would it be appropriate in a third way to say, "Argv can pass information into a program at runtime?"

No.

argv defines how the command line looks like to the process e.g., argv[0] sets one of the name for the process (another one is derived from the path to the actual executable). On POSIX, argv is a parameter for exec*() functions that is passed to C main(argc, argv) that is the entry point for a C program.

In other words, argv is used to invoke (start/run) the process but as @G Fetterman mentioned "at runtime" may refer to the whole process running time, not only the invocation time. argv may be known even before the process is running and argv usually stays the same after the process is started.




回答3:


Yes, that's correct. Consider the following code testargs.py:

import sys

print(sys.argv[1])

When you run this script as python testargs.py banana you see that the result prints "banana". Note that argv[0] is the script name, any argument given after that is argv[1], argv[2] and so on. For more sophisticated use of command line arguments, consider using the Argparse module which contains options add help docs and other features.

Edit: I only covered the invoked portion, not the runtime question.



来源:https://stackoverflow.com/questions/33727355/terminology-argv-invoking-a-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!