Program flow
To form the logical flow of a computer program the component data objects and the
commands that activate various operations are organised into a particular order. Just as this
book is written to be read left to right, top to bottom, commands in Python are interpreted
left to right and top to bottom. And it is also in this order that the operations are enacted.
Operations
So far we have described the common types of data that you will be dealing with in your
programs. To make a working program, however, you must be able to do more than
organise data; you have to work with it by performing operations that depend upon the
content of the data. A simple example of an operation, and one which we have already
used above, would be the addition of numbers. Operations are specific to the type of
object that they work upon, so you can do mathematics with numbers, but not strings.
Similarly, you can join two strings together to form a longer text. For the two operations
of adding numbers and linking strings you can use the same ‘+’ symbol in the Python code
to perform the two operations, but the result is always appropriate for the type of object.
x = '22'
y = '78'
print(x + y)
Operations in Python are appropriate to the type of data involved, so for the above example the
result is the text ‘2278’ not the number 100.
If an object is not internally modifiable, like the number 4 or “abc”, then when you
perform an operation you get a different object as the result. So joining two text strings
makes a new string. If an object is internally modifiable then an operation is allowed to
(although it doesn’t have to) alter the data within the object’s structure without making any
new structures. A clear example of this would be reversing the order of a list; the operation
doesn’t make any new data-containing object, it just moves the contents around internally.
Operations that are indicated within a Python program with symbols like + or * are
really just shorthand ways of activating a procedure that is built into the fabric of the
object being used. Thus when you do 2+5, the number object 2 has an internal procedure,
activated by the + symbol, that deals with addition of the other number. Such procedures
that are built into objects are called methods in the jargon, and these are a special kind of
what we later describe as functions. Because there are only a limited set of symbols that
can be sensibly used to indicate such inbuilt procedures, often you have to activate the
procedure (‘call the method’ in jargon) directly with a dot notation. For example, to
reverse the order of a list named myData, because there is no symbolic way of reversing
the list, you would issue the command myData.reverse(). With this notation, notice that
the method has a name
7
and that it is clearly associated with its list object using a dot ‘.’.
We use the brackets at the end of the command to actually activate the procedure. If we
simply issued the command myData.reverse then Python would interpret this as referring
to the method (the procedure) without actually running it. The brackets at the end of an
object’s method may also contain some data that the operation is going to work with. For
example, to put the number 6 onto the end of a list you can use the append operation that
is built into list objects, and the extra number goes in brackets: myData.append(6).
Where there is actually a neat symbolic way of representing an operation there will also
be an equivalent, albeit less elegant, version with the dot notation. As was illustrated in the
example given earlier, x+y can be written as x.__add__(y). Here, you can see that the
operation which the + symbol activates is internally called ‘__add__’. The plethora of
underscore ‘_’ symbols indicates that this method is inbuilt and normally hidden.
Do'stlaringiz bilan baham: |