Page 174 -
P. 174

python toolbox



                       Your Python Toolbox


                       You’ve got Chapter 4 under your                  ƒ  The strip() method removes
                       belt and you’ve added some key
     CHAPTER 4                                                          ƒ   ƒ  unwanted whitespace from strings.  CHAPTER 4
                       Python techiques to your toolbox.

                                                                        The file argument to the print()
                                                                        BIF controls where data is sent/saved.
                                                                        The finally suite is always executed
                                                                        no matter what exceptions occur within a
                                                                        try/except statement.
                                                                        ƒ  An exception object is passed into the
                    Python Lingo                                        an identifier using the as keyword.
                                                                        except suite and can be assigned to
                     • “Immutable types” - data types                   ƒ  The str() BIF can be used to access
                     in Python that, once assigned
                                                                        the stringed representation of any data
                      a value, cannot have that value
                                                                        object that supports the conversion.
                      changed.                                          ƒ  The locals() BIF returns a collection
                                                                        of variables within the current scope.
                       • “Pickling” - the process of                    ƒ  The in operator tests for membership.
                       saving a data object to persistence

                        storage.                                        ƒ  The “+” operator concatenates two
                        • “Unpickling” - the process of                 two numbers together when used with
                                                                        strings when used with strings but adds
                         restoring a saved data object
                                                                        numbers.
                         from persistence storage.
                                                                        The with statement automatically
                                                                        ƒ
                                                                        arranges to close all opened files, even
                                                                        when exceptions occur. The with
                                                                        statement uses the as keyword, too.
                                                                        ƒ  sys.stdout is what Python calls
                                                                        “standard output” and is available from
                                                                        the standard library’s sys module.
                                                                        ƒ  The standard library’s pickle module
                                                                        lets you easily and efficiently save and
                                                                        restore Python data objects to disk.

                                                                        ƒ  The pickle.dump() function saves
                                                                        data to disk.
                                                                        ƒ  The pickle.load() function
                                                                        restores data from disk.



           138    Chapter 4
   169   170   171   172   173   174   175   176   177   178   179