12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Binary data files are advanced and somewhat low-level tools that we won’t cover inmore detail here; for more help, see the <strong>Python</strong> library manual, or import and passstruct to the help function interactively. Also note that the binary file-processingmodes 'wb' and 'rb' can be used to process a simpler binary file such as an image oraudio file as a whole without having to unpack its contents.Other File ToolsThere are additional, more advanced file methods shown in Table 9-2, and evenmore that are not in the table. For instance, seek resets your current position in a file(the next read or write happens at that position), flush forces buffered output to bewritten out to disk (by default, files are always buffered), and so on.The <strong>Python</strong> standard library manual and the reference books described in the Prefaceprovide complete lists of file methods; for a quick look, run a dir or help callinteractively, passing in the word file (the name of the file object type). For morefile-processing examples, watch for the sidebar “Why You Will Care: File Scanners”in Chapter 13. It sketches common file-scanning loop code patterns with statementswe have not covered enough yet to use here.Also, note that although the open function and the file objects it returns are yourmain interface to external files in a <strong>Python</strong> script, there are additional file-like tools inthe <strong>Python</strong> toolset. Also available are descriptor files (integer file handles that supportlower-level tools such as file locking), found in the os module; sockets, pipes,and FIFO files (file-like objects used to synchronize processes or communicate overnetworks); access-by-key files known as “shelves” (used to store unaltered <strong>Python</strong>objects directly, by key); and more. See more advanced <strong>Python</strong> texts for additionalinformation on these file-like tools.Type Categories RevisitedNow that we’ve seen all of <strong>Python</strong>’s core built-in types in action, let’s wrap up ourobject types tour by taking a look at some of the properties they share.Table 9-3 classifies all the types we’ve seen according to the type categories introducedearlier. Objects share operations according to their category; for instance,strings, lists, and tuples all share sequence operations. Only mutable objects (listsand dictionaries) may be changed in-place; you cannot change numbers, strings, ortuples in-place. Files only export methods, so mutability doesn’t really apply tothem—they may be changed when written, but this isn’t the same as <strong>Python</strong> typeconstraints.182 | Chapter 9: Tuples, Files, and Everything Else

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!