Download TEAM LinG - LinuxTone.Org
Transcript
Chapter 5
Checking Your Parameters
The parameters that you intend to be used could be expecting different types than what they are given
when the function is called. For example, you could write a function that expects to be given a dictionary
but by accident is instead given a list, and your function will run until an operation unique to a dictionary is accessed. Then the program will exit because an exception will be generated. This is different
from some other languages, which try to ensure that the type of each parameter is known, and can be
checked to be correct.
Python does not check to see what kind of data it’s associating to the names in a function. In most cases
this isn’t a problem because an operation on the provided data will be specific to a type, and then fail to
work properly if the type of data that the name references is not correct.
For instance, if in_fridge is given a number instead of a dictionary, Python, when trying to access the
number as though it were a dictionary, will raise an error that the except: will not catch.. A TypeError
will be generated indicating that the type Python tried to operate on isn’t capable of doing what Python
expected:
>>> in_fridge(4, “cookies”)
Traceback (most recent call last):
File “<stdin>”, line 1, in ?
File “<stdin>”, line 7, in in_fridge
TypeError: unsubscriptable object
In this case, you’ve been shown a number being given to a function where you know that the function
expects to operate on a dictionary. No matter what, a number does not have a property where a name
can be used as the key to find a value. A number doesn’t have keys and it doesn’t have values. The idea
is that in any context, finding 4(“cookies”) can’t be done in Python, and so an exception is raised.
The term unsubscriptable is how Python indicates that it can’t find a way to follow a key to a value the
way it needs to with a dictionary. Subscripting is the term for describing when you access an element in
a list or a tuple as well as a dictionary, so you can encounter this error in any of those contexts.
This behavior — not requiring you to specifically define what type you expect, and allowing you to flexibly decide how you want to treat it — can be used to your advantage. It enables you to write a single
function that handles any kind of input that you want. You can write a single function that can take
more than one type as its parameter and then decide how the function should behave based on the type
it is given. Which approach you take depends on what you need to do in your own program.
To determine the type of some data, remember that you can use the type built-in function, which was
introduced in Chapter 2. Using the output of this, you can verify the type of variable in the beginning of
your functions:
def make_omelet(omelet_type):
“””This will make an omelet. You can either pass in a dictionary
that contains all of the ingredients for your omelet, or provide
a string to select a type of omelet this function already knows
about”””
if type(omelet_type) == type({}):
print “omelet_type is a dictionary with ingredients”
return make_food(omelet_type, “omelet”)
68
TEAM LinG
Related documents
appendices 1.0 project management
wxGlade manual
XO 200 S User Manual eng
A Python Book - Dave Kuhlman dot org
EVBUM2115 - NCV7703 Evaluation Board User`s Manual
it en de fr es nl istruzioni instructions bedienungsanleitung mode d
LES TARIFS des séries RED de SFR
VMware VCLOUD REQUEST MANAGER 1.0.0 User`s guide
D2.2 – Workflow Software Analysis and Comparison
Wiley Beginning Python
SECURITY ALERTS COLLECTING SYSTEM (SYRAS)