Why should sys.argv with a negative index allow printing same value as sys.argv[0]? That too, it allows this up to number of arguments passed.
So, a call to hello.py at developers.google.com, such as the one below (with 3 arguments including script name) : python hello.py Sumit Test
would allow accessing sys.argv[-1], [-2] and [-3] with all of them printing the same value as argv[0] i.e. hello.py, but argv[-4] will throw the expected error:
Traceback (most recent call last):
File "hello.py", line 35, in <module>
main()
File "hello.py", line 31, in main
print (sys.argv[-4])
IndexError: list index out of range
The code is:
import sys
# Define a main() function that prints a little greeting.
def main():
# Get the name from the command line, using 'World' as a fallback.
if len(sys.argv) >= 2:
name = sys.argv[1]
else:
name = 'World'
print ('Hello', name)
print (sys.argv[-3])
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()