Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 606 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
606
Dung lượng
3,96 MB
Nội dung
Learning Python Table of Contents Learning Python Credits About the Author Acknowledgements About the Reviewers www.PacktPub.com Support files, eBooks, discount offers, and more Why subscribe? Free access for Packt account holders Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Errata Piracy Questions Introduction and First Steps – Take a Deep Breath A proper introduction Enter the Python About Python Portability Coherence Developer productivity An extensive library Software quality Software integration Satisfaction and enjoyment What are the drawbacks? Who is using Python today? Setting up the environment Python 2 versus Python 3 – the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code organized How do we use modules and packages Python’s execution model Names and namespaces Scopes Object and classes Guidelines on how to write good code The Python culture A note on the IDEs Summary Built-in Data Types Everything is an object Mutable or immutable? That is the question Numbers Integers Booleans Reals Complex numbers Fractions and decimals Immutable sequences Strings and bytes Encoding and decoding strings Indexing and slicing strings Tuples Mutable sequences Lists Byte arrays Set types Mapping types – dictionaries The collections module Named tuples Defaultdict ChainMap Final considerations Small values caching How to choose data structures About indexing and slicing About the names Summary Iterating and Making Decisions Conditional programming A specialized else: elif The ternary operator Looping The for loop Iterating over a range Iterating over a sequence Iterators and iterables Iterating over multiple sequences The while loop The break and continue statements A special else clause Putting this all together Example 1 – a prime generator Example 2 – applying discounts A quick peek at the itertools module Infinite iterators Iterators terminating on the shortest input sequence Combinatoric generators Summary Functions, the Building Blocks of Code Why use functions? Reduce code duplication Splitting a complex task Hide implementation details Improve readability Improve traceability Scopes and name resolution The global and nonlocal statements Input parameters Argument passing Assignment to argument names don’t affect the caller Changing a mutable affects the caller How to specify input parameters Positional arguments Keyword arguments and default values Variable positional arguments Variable keyword arguments Keyword-only arguments Combining input parameters Avoid the trap! Mutable defaults Return values Returning multiple values A few useful tips Recursive functions Anonymous functions Function attributes Built-in functions One final example Documenting your code Importing objects Relative imports Summary Saving Time and Memory map, zip, and filter map zip filter Comprehensions Nested comprehensions Filtering a comprehension dict comprehensions set comprehensions Generators Generator functions Going beyond next The yield from expression Generator expressions Some performance considerations Don’t overdo comprehensions and generators Name localization Generation behavior in built-ins One last example Summary Advanced Concepts – OOP, Decorators, and Iterators Decorators A decorator factory Object-oriented programming The simplest Python class Class and object namespaces Attribute shadowing I, me, and myself – using the self variable Initializing an instance OOP is about code reuse Inheritance and composition Accessing a base class Multiple inheritance Method resolution order Static and class methods Static methods Class methods Private methods and name mangling The property decorator Operator overloading Polymorphism – a brief overview Writing a custom iterator Summary Testing, Profiling, and Dealing with Exceptions Testing your application The anatomy of a test Testing guidelines Unit testing Writing a unit test Mock objects and patching Assertions A classic unit test example Making a test fail Interface testing Comparing tests with and without mocks Boundaries and granularity A more interesting example Test-driven development Exceptions Profiling Python When to profile? Summary The Edges – GUIs and Scripts First approach – scripting The imports Parsing arguments The business logic Second approach – a GUI application The imports The layout logic The business logic Fetching the web page Saving the images Alerting the user How to improve the application? Where do we go from here? ... Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code organized How do we use modules and packages Python s execution model... Setting up the environment Python 2 versus Python 3 – the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program... Learning Python Table of Contents Learning Python Credits About the Author Acknowledgements About the Reviewers www.PacktPub.com