mark lutz python pocket reference, 5th edition 2014

Download Mark lutz python pocket reference, 5th edition 2014

Post on 24-Jul-2016




1 download

Embed Size (px)


Питон в кармане


  • Mark Lutz


    Python Pocket Reference

  • Python Pocket Reference, Fifth Editionby Mark Lutz

    Copyright 2014 Mark Lutz. All rights reserved.

    Printed in the United States of America.

    Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

    OReilly books may be purchased for educational, business, or sales promotionaluse. Online editions are also available for most titles ( For more information, contact our corporate/institutional sales department: 800-998-9938 or

    Editor: Rachel RoumeliotisProduction Editor: Kristen BrownCopyeditor: Richard CareyProofreader: Amanda KerseyIndexer: Lucie HaskinsCover Designer: Randy ComerInterior Designer: David Futato

    October 1998: First EditionJanuary 2002: Second EditionFebruary 2005: Third EditionOctober 2009: Fourth EditionFebruary 2014: Fifth Edition

    Revision History for the Fifth Edition:2014-01-17: First release

    See for release details.

    Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo areregistered trademarks of OReilly Media, Inc. Python Pocket Reference, the image of a rock python, and related trade dress are trademarks of OReilly Media,Inc.

    Many of the designations used by manufacturers and sellers to distinguish theirproducts are claimed as trademarks. Where those designations appear in thisbook, and OReilly Media, Inc. was aware of a trademark claim, the designationshave been printed in caps or initial caps.

    While every precaution has been taken in the preparation of this book, thepublisher and authors assume no responsibility for errors or omissions, or fordamages resulting from the use of the information contained herein.

    ISBN: 978-1-449-35701-6


  • Table of Contents

    Introduction 1Book Conventions 2Python Command-Line Usage 3

    Python Command Options 4Command-Line Program Specification 5Python 2.X Command Options 7

    Python Environment Variables 7Operational Variables 8Python Command Option Variables 9

    Python Windows Launcher Usage 10Launcher File Directives 10Launcher Command Lines 11Launcher Environment Variables 11

    Built-in Types and Operators 12Operators and Precedence 12Operator Usage Notes 14Operations by Category 16Sequence Operation Notes 20

    Specific Built-in Types 21Numbers 22


  • Strings 24Unicode Strings 42Lists 46Dictionaries 53Tuples 57Files 58Sets 63Other Types and Conversions 65

    Statements and Syntax 67Syntax Rules 67Name Rules 69

    Specific Statements 71The Assignment Statement 72The Expression Statement 76The print Statement 77The if Statement 80The while Statement 80The for Statement 80The pass Statement 81The break Statement 81The continue Statement 81The del Statement 81The def Statement 82The return Statement 86The yield Statement 87The global Statement 88The nonlocal Statement 89The import Statement 89The from Statement 93The class Statement 95The try Statement 97The raise Statement 99

    iv | Table of Contents

  • The assert Statement 101The with Statement 102Python 2.X Statements 104

    Namespace and Scope Rules 105Qualified Names: Object Namespaces 105Unqualified Names: Lexical Scopes 105Nested Scopes and Closures 107

    Object-Oriented Programming 108Classes and Instances 109Pseudoprivate Attributes 110New-Style Classes 111Formal Inheritance Rules 112

    Operator Overloading Methods 117Methods for All Types 118Methods for Collections (Sequences, Mappings) 123Methods for Numbers (Binary Operators) 125Methods for Numbers (Other Operations) 128Methods for Descriptors 129Methods for Context Managers 130Python 2.X Operator Overloading Methods 131

    Built-in Functions 134Python 2.X Built-in Functions 155

    Built-in Exceptions 161Superclasses: Categories 162Specific Exceptions 163Specific OSError Exceptions 167Warning Category Exceptions 169Warnings Framework 170Python 3.2 Built-in Exceptions 171Python 2.X Built-in Exceptions