Writing Idiomatic Python 3 by Jeff Knupp

By Jeff Knupp

Write. Idiomatic. Code.

Get pythonic from the beginning: concise advisor to idiomatic code; most sensible after one other textual content, yet matches a few courageous newcomers.

Show description

Read or Download Writing Idiomatic Python 3 PDF

Similar python books

Fundamentals of Python: From First Programs through Data Structures

In basics OF PYTHON: FROM FIRST courses via facts constructions, Washington and Lee collage professor Kenneth A. Lambert offers the entire vital themes in CS1 and CS2 in a single quantity. This within your budget layout presents teachers with a constant method of instructing introductory programming and information constructions over a customary two-term path series.

Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython

Python for facts research is anxious with the nuts and bolts of manipulating, processing, cleansing, and crunching info in Python. it's also a pragmatic, sleek creation to clinical computing in Python, adapted for data-intensive purposes. it is a ebook concerning the elements of the Python language and libraries you'll have to successfully resolve a vast set of information research difficulties.

Python and AWS

In case you intend to take advantage of Amazon internet providers (AWS) for distant computing and garage, Python is a perfect programming language for constructing functions and controlling your cloud-based infrastructure. This cookbook will get you begun with greater than dozen recipes for utilizing Python with AWS, in line with the author’s boto library.

Artificial Intelligence with Python

Construct real-world synthetic Intelligence functions with Python to intelligently have interaction with the realm round you approximately This publication Step into the superb international of clever apps utilizing this accomplished advisor input the realm of synthetic Intelligence, discover it, and create your personal purposes paintings via basic but insightful examples that would get you up and operating with synthetic Intelligence very quickly Who This booklet Is For This e-book is for Python builders who are looking to construct real-world man made Intelligence purposes.

Additional info for Writing Idiomatic Python 3

Example text

Python has two oft-overlooked built-in functions, chr and ord, that are used to perform these translations. 1 Harmful hash_value = 0 character_hash = { 'a': 97, 'b': 98, 'c': 99, # ... 4 Prefer the format function for formatting strings There are three general ways of formatting strings (that is, creating a string that is a mix of hard-coded strings and string variables). Easily the worst approach is to use the + operator to concatenate a mix of static strings and variables. Using “old-style” string formatting is slightly better.

The bare raise re-raises the exception that was caught. This way, your code runs and the user still gets useful information when something goes wrong. Of course, there are valid reasons one would need to ensure some block of code never generates an exception. Almost none of the idioms described in this book are meant to be mechanically followed: use your head. json() # If we need to make note of the exception, we # would write the function this way... json() except: # do some logging here, but don't handle the exception # ...

It’s also an idiom that sounds obvious when you hear or see it, but is not something those new to the language typically divine on their own.  attributes that do not require an instance of the class to be referred to). For example, it’s common for ORM’s like SQLAlchemy to require model classes to define a __tablename__ attribute. Given a Blog model class, the value of __tablename__ may be blog. __tablename__. Doing so can create problems in derived classes. __tablename__, calling the method on any class derived from Blog would return blog rather than the derived class’s __tablename__.

Download PDF sample

Rated 4.36 of 5 – based on 22 votes