You have more control in a program than just activating all of the written commands once
in written order. You can use control commands to divert the flow of the program’s
execution under certain conditions, to add loops to repeat the execution of certain
statements and to jump to a completely different part of the program, run some commands
programs. As a simple example you might wish to look at all of the elements of a list in
turn by using a repeating loop, performing the same operation on each of the values.
some commands and then jump back again would be described in Python jargon as a
function. In some older programming languages you can make the order of your program’s
execution jump about by using GOTO commands; which simply says that commands
from now on are executed from a specified line of code in the program. In contrast, a
function in Python is a section of code that is bundled together with a name. When the
activate those commands immediately. Only when the name of the function is used
appropriately in the main flow of the program’s execution are the commands from the
function run. At the end of the function’s execution the program flow goes back to the
point where the function was activated, often sending back some data from the function.
def convertToFahrenheit(celsius):
fahrenheit = celsius * 1.8 + 32.0
return fahrenheit
print(convertToFahrenheit(37.0))
A simple example of a Python function: how the function works is defined in an abstract way after
the def keyword, but the operation is only actually performed on real numbers at the last line.
Just as we write Python modules to store groups of commands as separate files that can
be useful in many separate situations, so too functions are written because they perform a
role that is useful in many different parts of a program. It is generally far better to write a
function to do a particular job once, and then activate or call that function wherever that
job needs to be performed, rather than writing several bits of code that do the same thing.
One note of caution with using Python functions is that they can be proportionally slow to
run compared to the regular flow of a Python program; so if speed is an issue things can
often be helped by removing unnecessary calls to functions. Also, functions are generally
only useful if you use them in more than one place in a program. If a procedure is only
ever going to be run in one part of a program you would usually put the required
commands directly into the program and not bother with a named function.
Although Python functions can exist on their own, they can also be linked to particular
kinds of data structures. A function that is linked to an object becomes a method of that
object (a procedure that belongs to the object), and can be executed in the same way as
any other method with the dot notation, as discussed earlier.
class UnitConverter:
def metresToFeet(self, m):
f = m / 0.3048
return f
def feetToMetres(self, f):
m = f * 0.3048
return m
converterObj = UnitConverter()
print(converterObj.metresToFeet(1.89))
print(converterObj.feetToMetres(6.0))
The definition of custom Python objects is specified using the class
keyword. Here we have defined a
rudimentary unit converter class of object, which itself contains function definitions. The actual
converter object is created by using the name of the class and from this object we can access the
conversion functions.
Do'stlaringiz bilan baham: