Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
English.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
14.4 Кб
Скачать

Executing Statements in Python

If you spend a lot of your day typing commands into a terminal, then you are used to

executing statements and, perhaps, redirecting the output to a file or to another Unix

command.

The Python examples probably looks a bit strange. You might be thinking, “What the heck is this import subprocess thing?” One of the powerful features of Python is its ability to import modules or other files that contain code and reuse them in a new program. If you are familiar with “sourcing” a file in Bash, then you will recognize some similarities. In this particular situation, all that is important to know is that you import the subprocess and use it in the syntax that is shown.

You can run any shell command in Python just as it would be run with Bash. Given this bit of information, you can now create a Python version of ls. And output will be the same. While this may seem silly, (and it is silly actually), it gives you a good idea of a common use of Python in systems programming. Often, you use Python to “wrap” other scripts or Unix commands. Given this new bit of information, you could happily start writing some basic scripts if you just put one command after another in a file and ran it.

Python version of the script, we imported the subprocess module because it already contained the code to make system calls in Python. As I mentioned earlier, importing a module like subprocess is just importing a file that contains code you can use. You can create your own module or file and reuse code you have written in the same way you import subprocess . Importing is not magic at all, it is just a file with some code in it. One of the nice things about the IPython shell that you have open is its ability to inspect inside modules and files, and see the attributes that are available inside them. In Unix terms, this is a lot like running the ls command inside of /usr/bin. If you happen to be on a new system such as Ubuntu or Solaris, and you are used to Red Hat, you might do an ls of /usr/bin to see if tools such as wget, curl, or lynx are available. If you want to use a tool you find inside /usr/bin, you would

simply type /usr/bin/wget , for example.

Using Functions in Python

In the previous section we went through executing statements one after another, which is pretty useful, because it means we were able to automate something that we would normally have to do manually. The next step to automating our code execution is to create functions. If you are not already familiar with writing functions in Bash or another language, then one way to think about functions is as miniscripts. A function

allows you to create blocks of statements that get called in groups that live inside of the function. This is quite a bit like the Bash script we wrote in which there were two

commands enclosed in a script. One of the differences between a Bash script and a function is that you can include many function scripts. Ultimately, you can have multiple functions that group statements together in a script, and then that group of statements can be called to run a miniprogram at the proper time in your script. At this point, we need to talk about the topic of whitespace. In Python, a uniform level of indentation must be maintained in nesting code. In another language, like Bash, when you define a function you put brackets around the code inside of a function. With Python, you must indent the code inside of the bracket. This can trip up newcomers to the language, at first, but after a while it will grow on you, and you will realize that this encourages readable code. If you have trouble getting any of these examples to work interactively, make sure you refer to the actual source code to see the proper indentation level. The most common practice is to set a tab to indent exactly four spaces.

When you try to write a “print Hello, World!” function, you can see that putting a print statement in a function allows you not only to call the function later but also to call it as many times as we want.

Functions are not magic, and writing multiple functions interactively is a great way to

take away the mystery if this is your first experience with them.

You can simply placed these statements inside functions and then used the main func-

tion to call them all at once. If you are not familiar with this style, you might not have

known that it is common to create several functions inside a script and then call them

all with one main function. One of many reasons for this is that if you decide to reuse

this script for another program, you can either call the functions independently or together with the main method. The key is that you decide after the module is

imported.

When there is no control flow, or main function, then all of the code gets executed immediately when it is imported. This may be OK for a one-off script, but if you plan

to create reusable tools, and you should, then it is a good practice to create functions

that encapsulate specific actions, and then have a main function that executes the whole program.

At this point, you are now a programmer capable of writing simple functions in both

Bash and Python. Programmers learn by doing, though, so at this point we highly rec-

ommend that you change the system calls in these two Bash and Python programs and make them your own. Give yourself some bonus points if you add several new functions to the script and call them from a main function.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]