Idioms and Anti-Idioms in Python

Similar documents
Exception Handling. Genome 559

ECE 364 Software Engineering Tools Lab. Lecture 8 Python: Advanced I

Class extension and. Exception handling. Genome 559

Class extension and. Exception handling. Genome 559

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

CIS192 Python Programming

CIS192 Python Programming

CIS192 Python Programming

CS Programming Languages: Python

Modules and scoping rules

Python for Informatics

Python for Non-programmers

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Python Tutorial. Day 2

Textbook. Topic 8: Files and Exceptions. Files. Types of Files

Try and Error. Python debugging and beautification

CS 11 python track: lecture 3. n Today: Useful coding idioms

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

1 Decorators. 2 Descriptors. 3 Static Variables. 4 Anonymous Classes. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers July 13, / 19

Chapter 9: Dealing with Errors

The current topic: Python. Announcements. Python. Python

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Python File Modes. Mode Description. Open a file for reading. (default)

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

LECTURE 4 Python Basics Part 3

Python debugging and beautification

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Exceptions & error handling in Python 2 and Python 3

Errors. And How to Handle Them

Introduction to Python (All the Basic Stuff)

CIS192 Python Programming

Introduction to Python

CSE : Python Programming

CS558 Programming Languages

1 Strings (Review) CS151: Problem Solving and Programming

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Jython. secondary. memory

Exceptions CS GMU

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

Software Design and Analysis for Engineers

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

CSc 120. Introduction to Computer Programming II. 07: Excep*ons. Adapted from slides by Dr. Saumya Debray

Designing Robust Classes

Introduction to python

CS558 Programming Languages

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python

An Introduction to Python

Embedding a Python interpreter into your program

redis-lua Documentation

Introduction to Python

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

What we already know. more of what we know. results, searching for "This" 6/21/2017. chapter 14

3 Nonlocal Exit. Quiz Program Revisited

CS 3 Introduction to Software Engineering. 3: Exceptions

Senthil Kumaran S

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

CS 11 python track: lecture 2

features of Python 1.5, including the features earlier described in [2]. Section 2.6 summarizes what is new in Python The class and the class

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Introduction to Computer Programming for Non-Majors

Python Programming, bridging course 2011

Programming Languages

Cosmology with python: Beginner to Advanced in one week. Tiago Batalha de Castro

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

FILE HANDLING AND EXCEPTIONS

Slide Set 15 (Complete)

4.3 FURTHER PROGRAMMING

Scripting With Jython

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Control Structures 1 / 17

Modules and Programs 1 / 14

Computer Science 9608 (Notes) Chapter: 4.3 Further programming

These are notes for the third lecture; if statements and loops.

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Libgdb. Version 0.3 Oct Thomas Lord

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Introduction to Computer Programming for Non-Majors

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

GNU ccscript Scripting Guide IV

Swift-Tcl. Bridging between Swift and Tcl. Why Swift? - Swift vs C and Swift vs Tcl. Peter da Silva. Tcl October 24, 2016

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

CHAPTER 2. Troubleshooting CGI Scripts

Transcription:

Idioms and Anti-Idioms in Python Release 2.6.3 Guido van Rossum Fred L. Drake, Jr., editor October 06, 2009 Python Software Foundation Email: docs@python.org Contents 1 Language Constructs You Should Not Use i 1.1 from module import *........................................... ii Inside Function Definitions........................................ ii At Module Level............................................. ii When It Is Just Fine............................................ ii 1.2 Unadorned exec, execfile() and friends.............................. ii 1.3 from module import name1, name2.................................... iii 1.4 except:................................................... iii 2 Exceptions iv 3 Using the Batteries v 4 Using Backslash to Continue Statements v Author Moshe Zadka This document is placed in the public domain. Abstract This document can be considered a companion to the tutorial. It shows how to use Python, and even more importantly, how not to use Python. 1 Language Constructs You Should Not Use While Python has relatively few gotchas compared to other languages, it still has some constructs which are only useful in corner cases, or are plain dangerous.

1.1 from module import * Inside Function Definitions from module import * is invalid inside function definitions. While many versions of Python do not check for the invalidity, it does not make it more valid, no more than having a smart lawyer makes a man innocent. Do not use it like that ever. Even in versions where it was accepted, it made the function execution slower, because the compiler could not be certain which names are local and which are global. In Python 2.1 this construct causes warnings, and sometimes even errors. At Module Level While it is valid to use from module import * at module level it is usually a bad idea. For one, this loses an important property Python otherwise has you can know where each toplevel name is defined by a simple search function in your favourite editor. You also open yourself to trouble in the future, if some module grows additional functions or classes. One of the most awful question asked on the newsgroup is why this code: f = open("www") f.read() does not work. Of course, it works just fine (assuming you have a file called www.) But it does not work if somewhere in the module, the statement from os import * is present. The os module has a function called open() which returns an integer. While it is very useful, shadowing builtins is one of its least useful properties. Remember, you can never know for sure what names a module exports, so either take what you need from module import name1, name2, or keep them in the module and access on a per-need basis import module;print module.name. When It Is Just Fine There are situations in which from module import * is just fine: The interactive prompt. For example, from math import * makes Python an amazing scientific calculator. When extending a module in C with a module in Python. When the module advertises itself as from import * safe. 1.2 Unadorned exec, execfile() and friends The word unadorned refers to the use without an explicit dictionary, in which case those constructs evaluate code in the current environment. This is dangerous for the same reasons from import * is dangerous it might step over variables you are counting on and mess up things for the rest of your code. Simply do not do that. Bad examples: >>> for name in sys.argv[1:]: >>> exec "%s=1" % name >>> def func(s, **kw): >>> for var, val in kw.items(): >>> exec "s.%s=val" % var # invalid! >>> execfile("handler.py") >>> handle()

Good examples: >>> d = {} >>> for name in sys.argv[1:]: >>> d[name] = 1 >>> def func(s, **kw): >>> for var, val in kw.items(): >>> setattr(s, var, val) >>> d={} >>> execfile("handle.py", d, d) >>> handle = d[ handle ] >>> handle() 1.3 from module import name1, name2 This is a don t which is much weaker than the previous don t s but is still something you should not do if you don t have good reasons to do that. The reason it is usually bad idea is because you suddenly have an object which lives in two separate namespaces. When the binding in one namespace changes, the binding in the other will not, so there will be a discrepancy between them. This happens when, for example, one module is reloaded, or changes the definition of a function at runtime. Bad example: # foo.py a = 1 # bar.py from foo import a if something(): a = 2 # danger: foo.a!= a Good example: # foo.py a = 1 # bar.py import foo if something(): foo.a = 2 1.4 except: Python has the except: clause, which catches all exceptions. Since every error in Python raises an exception, this makes many programming errors look like runtime problems, and hinders the debugging process. The following code shows a great example: foo = opne("file") # misspelled "open" except: sys.exit("could not open file!") The second line triggers a NameError which is caught by the except clause. The program will exit, and you will have no idea that this has nothing to do with the readability of "file". The example above is better written

foo = opne("file") # will be changed to "open" as soon as we run it except IOError: sys.exit("could not open file") There are some situations in which the except: clause is useful: for example, in a framework when running callbacks, it is good not to let any callback disturb the framework. 2 Exceptions Exceptions are a useful feature of Python. You should learn to raise them whenever something unexpected occurs, and catch them only where you can do something about them. The following is a very popular anti-idiom if not os.path.exists(file): print "file not found" sys.exit(1) return open(file).readline() Consider the case the file gets deleted between the time the call to os.path.exists() is made and the time open() is called. That means the last line will throw an IOError. The same would happen if file exists but has no read permission. Since testing this on a normal machine on existing and non-existing files make it seem bugless, that means in testing the results will seem fine, and the code will get shipped. Then an unhandled IOError escapes to the user, who has to watch the ugly traceback. Here is a better way to do it. return open(file).readline() except (IOError, OSError): print "file not found" sys.exit(1) In this version, *either* the file gets opened and the line is read (so it works even on flaky NFS or SMB connections), or the message is printed and the application aborted. Still, get_status() makes too many assumptions that it will only be used in a short running script, and not, say, in a long running server. Sure, the caller could do something like status = get_status(log) except SystemExit: status = None So, try to make as few except clauses in your code those will usually be a catch-all in the main(), or inside calls which should always succeed. So, the best version is probably return open(file).readline() The caller can deal with the exception if it wants (for example, if it tries several files in a loop), or just let the exception filter upwards to its caller.

The last version is not very good either due to implementation details, the file would not be closed when an exception is raised until the handler finishes, and perhaps not at all in non-c implementations (e.g., Jython). fp = open(file) return fp.readline() finally: fp.close() 3 Using the Batteries Every so often, people seem to be writing stuff in the Python library again, usually poorly. While the occasional module has a poor interface, it is usually much better to use the rich standard library and data types that come with Python than inventing your own. A useful module very few people know about is os.path. It always has the correct path arithmetic for your operating system, and will usually be much better than whatever you come up with yourself. Compare: # ugh! return dir+"/"+file # better return os.path.join(dir, file) More useful functions in os.path: basename(), dirname() and splitext(). There are also many useful builtin functions people seem not to be aware of for some reason: min() and max() can find the minimum/maximum of any sequence with comparable semantics, for example, yet many people write their own max()/min(). Another highly useful function is reduce(). A classical use of reduce() is something like import sys, operator nums = map(float, sys.argv[1:]) print reduce(operator.add, nums)/len(nums) This cute little script prints the average of all numbers given on the command line. The reduce() adds up all the numbers, and the rest is just some pre- and postprocessing. On the same note, note that float(), int() and long() all accept arguments of type string, and so are suited to parsing assuming you are ready to deal with the ValueError they raise. 4 Using Backslash to Continue Statements Since Python treats a newline as a statement terminator, and since statements are often more than is comfortable to put in one line, many people do: if foo.bar()[ first ][0] == baz.quux(1, 2)[5:9] and \ calculate_number(10, 20)!= forbulate(500, 360): pass You should realize that this is dangerous: a stray space after the \ would make this line wrong, and stray spaces are notoriously hard to see in editors. In this case, at least it would be a syntax error, but if the code was: value = foo.bar()[ first ][0]*baz.quux(1, 2)[5:9] \ + calculate_number(10, 20)*forbulate(500, 360)

then it would just be subtly wrong. It is usually much better to use the implicit continuation inside parenthesis: This version is bulletproof: value = (foo.bar()[ first ][0]*baz.quux(1, 2)[5:9] + calculate_number(10, 20)*forbulate(500, 360))