The Influence of C in Python
00:00 When the Python language was originally being designed, the creators were very explicit that Python would retain a lot of compatibility with C and a lot of influence from C. In this lesson, I’ll take you through how that C influence on Python manifests itself in command line applications in Python. All right.
So, I have a C file here in my editor called
main.c, and I’m just going to compile and run it so that you can see how it works. I think once you see how it works, you’ll understand the code as well.
into an executable called
main. If you’re interested in the process of C compilation, I would encourage you to read a little bit more about it. Generally, for Linux and Mac terminals,
gcc is the way to go, but you’ll have to use something called
clang in Windows, or perhaps a Linux Subsystem for Windows, but there’s a lot of complexity there that I would encourage you just to read a little bit more about yourself.
So now, I have an executable called
main that I can just run directly using the
./ notation. And as you can see, it prints out
C Main, and then prints out that there’s
1 argument, and that that argument is the actual filename
It might be surprising that that’s considered an argument, but remember that really what’s happening here is
main is the file being executed, so it’s an argument to the actual shell command that executes files.
So, that’s counted as an argument. But it will still be counted as an argument even if I pass in a few more arguments. As you can see now, there are now
5 arguments, including this original filename, as well as each whitespace-separated string that I pass in here on the command line. So now looking back at the code, as you can see, what this relies on is two parameters passed to the
main() function, and these are passed in by the system itself.
Those parameters are the integer
argc, for arg count, and that’s just the number of arguments, including that filename, and then a character pointer array or, in other words, a string array—because that’s how C handles strings—called
And as you can also probably notice, there’s no need anymore for an arg count, or an
argc, because you can just use the length of
sys.argv , because Python provides that to you natively. So, there’s definitely some syntactic sugar going on here.
One thing that I should note here is that this colon greater than
:>6) notation that I’m using within this format string is just to generate the nice spacing that you see below in the terminal. It’s to pad the space for numbers so that if I were to have some larger numbers in there, it would still show with the same spacing for the arguments.
I can use Python to run
python main.py and as you can see, it prints out
Python Main, still one argument—
main.py is still treated as an argument, except maybe, in this case, it’s more obvious why it’s an argument, because it’s being passed as an argument to the
And as you can see,
5 arguments passed, and all of them are now printed out to the command line. So, that’s generally how these command line interfaces work, and in later lessons, I’m going to get into much more detail on
sys.argv, how to handle arguments passed in, and what to do with exception handling, for example.
Become a Member to join the conversation.