from sys import argv - what is the function of “script”

后端 未结 4 1739
悲哀的现实
悲哀的现实 2020-12-30 04:35

I am reading \"Learn Python the Hard Way\" and was confused by the \"script\" part of the second line.

from sys import argv
script, filename = argv
         


        
4条回答
  •  孤独总比滥情好
    2020-12-30 05:24

    argv is a list of the arguments to your program. Standard shell behavior includes the name of the program itself as the first argument in argv.

    Python can assign multiple values at once if the number of variables on the left hand side equals the size of the list on the right hand side (it can also handle more cases, but that is the most basic). E.g.

    script, filename = argv
    

    is the same as

    script = argv[0]
    filename = argv[1]
    

    Note also that that script will raise a ValueError if argv does not have exactly two elements.

提交回复
热议问题