Recently, there have been a few proposals to change Python's syntax to make it easier to avoid break and continue statements.

The reasoning seems to be that many people are taught never to use break and continue, or to only have a single break in any loop. Some of these people are in fact forced to follow these rules for class assignments.

These rules are ludicrous. The stdlib has hundreds of break and continue statements. There are dozens of them in the official tutorial, the library reference docs, Guido's blogs, etc. The reasons for avoiding break and continue in other languages don't apply to Python, and many of the ways people use to avoid them don't even exist in Python. Using break and continue appropriately is clearly Pythonic.

The python-ideas community tracked these rules back to some misguided attempts to apply the rules from MISRA-C to Python.

It should be obvious that C and Python are very different languages, with different appropriate idioms, so applying MISRA-C to Python is insanely stupid. But, in case anyone doesn't get it, I've gone through the requirements and recommendations in MISRA-C 1998. By my count, more than half of them don't even mean anything in Python (e.g., rules about switch statements or #define macros), and more than half of the rest are bad rules that would encourage non-Pythonic code.

I've paraphrased them, both to avoid violating the copyright on the document, and to express them in Python terms:
  • 8. req: No unicode, only bytes, especially in literals. (In other words, '\u5050' is bad; b'\xe5\x81\x90' is good.)
  • 12. rcm: Don't use the same identifier in multiple namespaces. (For example, io.open and gzip.open should not have the same name, nor should two different classes ever have members or methods with the same name.)
  • 13. rcm: Never use int when you can use long. (Only applies to Python 2.x.)
  • 18. rcm: Suffix numeric literals. (Only applies to Python 2.x.)
  • 20. req: Declare all variables and functions at the top of a module or function.
  • 31. req: Use curly braces in all initializers. (Sorry, no lists allowed, just dicts and sets.)
  • 33. req: Never use a user-defined function call on the right side of logical and/or.
  • 34. req: Never use anything but a primary expression on either side of logical and/or.
  • 37. req.: Never use bitwise operators on signed types (like int).
  • 47. rcm: Never rely on operator precedence rules.
  • 48. rcm: Use explicit conversions when performing arithmetic between multiple types.
  • 49. rcm: Always test non-bool values against False instead of relying on truthiness.
  • 53. req: All statements should have a side-effect.
  • 57. req: No continue.
  • 58. req: No break.
  • 59. req: No one-liner if and loop bodies.
  • 60. rcm: No if/elif without else.
  • 67. rcm: Don't rebind the iterator in a for loop.
  • 68. req: All functions must be at file scope.
  • 69. req: Never use *args in functions.
  • 70. req: No recursion.
  • 82. rcm: Functions should have a single point of exit.
  • 83. req: No falling off the end of a function to return None.
  • 86. rcm: Always test function returns for errors.
  • 104. req: No passing functions around.
  • 118. req: Never allocate memory on the heap.
  • 119. req: Never rely on the errno in an OSError (or handle FileNotFoundError separately, etc.).
  • 121. req: Don't use locale functions.
  • 123. req: Don't use signals.
  • 124. req: Don't use stdin, stdout, etc., including print.
  • 126. req: Don't use sys.exit, os.environ, os.system, or subprocess.*.
  • 127. req: Never use Unix-style timestamps (e.g., time.time()).


1

View comments

  1. "67. rcm: Don't rebind the iterator in a for loop." - it won't choke your program like it would in C - but it is something that could be avoided in Python code in general. Of course that would be common sense, no need for a "rule" for that.

    ReplyDelete
Hybrid Programming
Hybrid Programming
5
Greenlets vs. explicit coroutines
Greenlets vs. explicit coroutines
6
ABCs: What are they good for?
ABCs: What are they good for?
1
A standard assembly format for Python bytecode
A standard assembly format for Python bytecode
6
Unified call syntax
Unified call syntax
8
Why heapq isn't a type
Why heapq isn't a type
1
Unpacked Bytecode
Unpacked Bytecode
3
Everything is dynamic
Everything is dynamic
1
Wordcode
Wordcode
1
For-each loops should define a new variable
For-each loops should define a new variable
4
Views instead of iterators
Views instead of iterators
2
How lookup _could_ work
How lookup _could_ work
2
How lookup works
How lookup works
7
How functions work
How functions work
2
Why you can't have exact decimal math
Why you can't have exact decimal math
2
Can you customize method resolution order?
Can you customize method resolution order?
1
Prototype inheritance is inheritance
Prototype inheritance is inheritance
1
Pattern matching again
Pattern matching again
The best collections library design?
The best collections library design?
1
Leaks into the Enclosing Scope
Leaks into the Enclosing Scope
2
Iterable Terminology
Iterable Terminology
8
Creating a new sequence type is easy
Creating a new sequence type is easy
2
Going faster with NumPy
Going faster with NumPy
2
Why isn't asyncio too slow?
Why isn't asyncio too slow?
Hacking Python without hacking Python
Hacking Python without hacking Python
1
How to detect a valid integer literal
How to detect a valid integer literal
2
Operator sectioning for Python
Operator sectioning for Python
1
If you don't like exceptions, you don't like Python
If you don't like exceptions, you don't like Python
2
Spam, spam, spam, gouda, spam, and tulips
Spam, spam, spam, gouda, spam, and tulips
And now for something completely stupid…
And now for something completely stupid…
How not to overuse lambda
How not to overuse lambda
1
Why following idioms matters
Why following idioms matters
1
Cloning generators
Cloning generators
5
What belongs in the stdlib?
What belongs in the stdlib?
3
Augmented Assignments (a += b)
Augmented Assignments (a += b)
11
Statements and Expressions
Statements and Expressions
3
An Abbreviated Table of binary64 Values
An Abbreviated Table of binary64 Values
1
IEEE Floats and Python
IEEE Floats and Python
Subtyping and Ducks
Subtyping and Ducks
1
Greenlets, threads, and processes
Greenlets, threads, and processes
6
Why don't you want getters and setters?
Why don't you want getters and setters?
8
The (Updated) Truth About Unicode in Python
The (Updated) Truth About Unicode in Python
1
How do I make a recursive function iterative?
How do I make a recursive function iterative?
1
Sockets and multiprocessing
Sockets and multiprocessing
Micro-optimization and Python
Micro-optimization and Python
3
Why does my 100MB file take 1GB of memory?
Why does my 100MB file take 1GB of memory?
1
How to edit a file in-place
How to edit a file in-place
ADTs for Python
ADTs for Python
5
A pattern-matching case statement for Python
A pattern-matching case statement for Python
2
How strongly typed is Python?
How strongly typed is Python?
How do comprehensions work?
How do comprehensions work?
1
Reverse dictionary lookup and more, on beyond z
Reverse dictionary lookup and more, on beyond z
2
How to handle exceptions
How to handle exceptions
2
Three ways to read files
Three ways to read files
2
Lazy Python lists
Lazy Python lists
2
Lazy cons lists
Lazy cons lists
1
Lazy tuple unpacking
Lazy tuple unpacking
3
Getting atomic writes right
Getting atomic writes right
Suites, scopes, and lifetimes
Suites, scopes, and lifetimes
1
Swift-style map and filter views
Swift-style map and filter views
1
Inline (bytecode) assembly
Inline (bytecode) assembly
Why Python (or any decent language) doesn't need blocks
Why Python (or any decent language) doesn't need blocks
18
SortedContainers
SortedContainers
1
Fixing lambda
Fixing lambda
2
Arguments and parameters, under the covers
Arguments and parameters, under the covers
pip, extension modules, and distro packages
pip, extension modules, and distro packages
Python doesn't have encapsulation?
Python doesn't have encapsulation?
3
Grouping into runs of adjacent values
Grouping into runs of adjacent values
dbm: not just for Unix
dbm: not just for Unix
How to use your self
How to use your self
1
Tkinter validation
Tkinter validation
7
What's the deal with ttk.Frame.__init__(self, parent)
What's the deal with ttk.Frame.__init__(self, parent)
1
Does Python pass by value, or by reference?
Does Python pass by value, or by reference?
9
"if not exists" definitions
"if not exists" definitions
repr + eval = bad idea
repr + eval = bad idea
1
Solving callbacks for Python GUIs
Solving callbacks for Python GUIs
Why your GUI app freezes
Why your GUI app freezes
21
Using python.org binary installations with Xcode 5
Using python.org binary installations with Xcode 5
defaultdict vs. setdefault
defaultdict vs. setdefault
1
Lazy restartable iteration
Lazy restartable iteration
2
Arguments and parameters
Arguments and parameters
3
How grouper works
How grouper works
1
Comprehensions vs. map
Comprehensions vs. map
2
Basic thread pools
Basic thread pools
Sorted collections in the stdlib
Sorted collections in the stdlib
4
Mac environment variables
Mac environment variables
Syntactic takewhile?
Syntactic takewhile?
4
Can you optimize list(genexp)
Can you optimize list(genexp)
MISRA-C and Python
MISRA-C and Python
1
How to split your program in two
How to split your program in two
How methods work
How methods work
3
readlines considered silly
readlines considered silly
6
Comprehensions for dummies
Comprehensions for dummies
Sockets are byte streams, not message streams
Sockets are byte streams, not message streams
9
Why you don't want to dynamically create variables
Why you don't want to dynamically create variables
7
Why eval/exec is bad
Why eval/exec is bad
Iterator Pipelines
Iterator Pipelines
2
Why are non-mutating algorithms simpler to write in Python?
Why are non-mutating algorithms simpler to write in Python?
2
Sticking with Apple's Python 2.7
Sticking with Apple's Python 2.7
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.