Questions tagged [pep]

Python Enhancement Proposals are used to propose and document Python language features, development processes and best practices. Use [pep8] for questions about the style guide for Python code.

Python Enhancement Proposals are used to propose and document Python language features, development processes and best practices.

Some important PEPs are:

At a given moment, the status of a PEP can be any one of Draft, Deferred, Accepted, Rejected, Withdrawn, Accepted, Final or Replaced. Informational PEPs which are expected to continue being updated may alternatively have a status of Active.

This flowchart shows how the status of a PEP may evolve:

enter image description here

A public Mercurial repository contains a record of changes to PEPs.

Use for questions about the style guide for Python code.

211 questions
289
votes
6 answers

Why is there no xrange function in Python3?

Recently I started using Python3 and it's lack of xrange hurts. Simple example: 1) Python2: from time import time as t def count(): st = t() [x for x in xrange(10000000) if x%4 == 0] et = t() print et-st count() 2) Python3: from time import…
catalesia
  • 3,100
  • 2
  • 11
  • 9
232
votes
4 answers

E731 do not assign a lambda expression, use a def

I get this pep8 warning whenever I use lambda expressions. Are lambda expressions not recommended? If not why?
Kechit Goyal
  • 3,002
  • 2
  • 16
  • 19
229
votes
8 answers

What's the function like sum() but for multiplication? product()?

Python's sum() function returns the sum of numbers in an iterable. sum([3,4,5]) == 3 + 4 + 5 == 12 I'm looking for the function that returns the product instead. somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60 I'm pretty sure such a function exists,…
Patrick McElhaney
  • 52,844
  • 37
  • 123
  • 157
146
votes
8 answers

Better to 'try' something and catch the exception or test if it's possible first to avoid an exception?

Should I test if something is valid or just try to do it and catch the exception? Is there any solid documentation saying that one way is preferred? Is one way more pythonic? For example, should I: if len(my_list) >= 4: x = my_list[3] else: …
chown
  • 48,838
  • 16
  • 128
  • 167
82
votes
3 answers

Data Classes vs typing.NamedTuple primary use cases

Long story short PEP-557 introduced data classes into Python standard library, that basically can fill the same role as collections.namedtuple and typing.NamedTuple. And now I'm wondering how to separate the use cases in which namedtuple is still a…
Oleh Rybalchenko
  • 3,977
  • 2
  • 13
  • 30
70
votes
7 answers

Which PEP's are must reads?

I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?
Michael Fairley
  • 12,490
  • 4
  • 23
  • 23
46
votes
14 answers

What PEP 8 guidelines do you ignore, and which ones do you stick to?

Over the years, the more Python I write, the more I find myself agreeing with most of the guidelines, though I consistently and intentionally break some for my own reasons. I'd be curious to know what in PEP 8 (or other PEPs too maybe) people…
user349594
28
votes
3 answers

Python PEP: blank line after function definition?

I can't find any PEP reference to this detail. There has to be a blank line after function definition? Should I do this: def hello_function(): return 'hello' or shoud I do this: def hello_function(): return 'hello' The same question…
bgusach
  • 13,019
  • 10
  • 44
  • 61
27
votes
3 answers

Triple-double quote v.s. Double quote

What is the preferred way to write Python doc string? """ or " In the book Dive Into Python, the author provides the following example: def buildConnectionString(params): """Build a connection string from a dictionary of parameters. …
Mingyu
  • 26,145
  • 13
  • 50
  • 58
26
votes
1 answer

How can I use setuptools to generate a console_scripts entry point which calls `python -m mypackage`?

I am trying to be a good Pythonista and following PEP 338 for my package I plan on deploying. I am also trying to generate my executable scripts upon python setuptools install using setuptools entry_points{'console_scripts': ... } options. How can…
25
votes
3 answers

How to write a pep8 configuration (pep8.rc) file?

I found the documentation for pep8 but wasn't able to understand how to write these. I couldn't even find any examples with options other than setting max-line-length and ignore. I am trying to write a .pep8.rc file in which, among other things, I…
Aditya Srivastava
  • 408
  • 1
  • 6
  • 11
21
votes
3 answers

What's the difference on docstrings with triple SINGLE quotes and triple DOUBLE quotes?

I was just wondering what is the difference between two ways of writing Python Docstrings (__doc__): three single quotes: ''' Comment goes here ''' three double quotes: """ Comment goes here """ Is there any subtle difference in the way doc…
prashu
  • 509
  • 2
  • 4
  • 10
20
votes
2 answers

Tool for automatically check docstring style according to PEP257

Tools like pep8 can check source code style, but they don't check if docstrings are fromatted according to pep257, pep287. Are there such tools? Update I decided to implement such a static analysis tool on my own,…
Vladimir Keleshev
  • 11,246
  • 15
  • 57
  • 88
19
votes
1 answer

How / why does Python type hinting syntax work?

I have just seen the following example in PEP 484: def greeting(name: str) -> str: return 'Hello ' + name print(greeting('Martin')) print(greeting(1)) As expected, this does not work in Python 2: File "test.py", line 1 def greeting(name:…
Martin Thoma
  • 91,837
  • 114
  • 489
  • 768
18
votes
1 answer

Coding style (PEP8) - Module level "dunders"

Definition of "Dunder" (Double underscore): http://www.urbandictionary.com/define.php?term=Dunder I have a question according the placement of module level "dunders" (like __all__, __version__, __author__ etc.) in Python code. The question came up…
linusg
  • 5,719
  • 4
  • 24
  • 68
1
2 3
14 15