Page 248 -
P. 248
python toolbox
Your Python Toolbox
You’ve got Chapter 6 under your
belt and you’ve added some key Create a empty dictionary using the
CHAPTER 6 dict() factory function or using {}. CHAPTER 6
Python techiques to your toolbox.
To access the value associated with
the key Name in a dictionary called
Python Lingo
person, use the familiar square bracket
• “Dictionary” - a built-in data notation: person['Name'].
Like list and set, a Python’s dictionary
structure that allows you to
dynamically grows as new data is added
associate data values with keys.
to the data structure.
• “Key” - the look-up part of Populate a dictionary as you go:
new_d = {} or new_d = dict()
the dictionary. and then
• “Value” - the data part of the d['Name'] = 'Eric Idle'
dictionary (which can be any value,
or do the same thing all in the one go:
including another data structure).
new_d = {'Name': 'Eric
Idle'}
The class keyword lets you define a
class.
More Python Lingo Class methods (your code) are defined in
much the same way as functions, that is,
with the def keyword.
• “self” - a method argument
that always refers to the current Class attributes (your data) are just like
object instance. variables that exist within object instances.
The __init__() method can be
defined within a class to initialize object
instances.
Every method defined in a class must
provide self as its first argument.
Every attribute in a class must be prefixed
with self. in order to associate it data
with its instance.
Classes can be built from scratch or can
inherit from Python’s built-in classes or
from other custom classes.
Classes can be put into a Python module
and uploaded to PyPI.
212 Chapter 6