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
   243   244   245   246   247   248   249   250   251   252   253