Live data from Hacker News

Python best practices

fantascienza.net

11–20 of 41 posts

Re: Python best practices

#11
Most of that stuff is a bit silly but I learned python before it supported verbose regular expressions so I found this example useful.

  finder = re.compile(r"""
    ^ \s*         # start at beginning+ opt spaces
    ( [\[\]] )    # Group 1: opening bracket
    \s*           # optional spaces
    ( [-+]? \d+ ) # Group 2: first number
    \s* , \s*     # opt spaces+ comma+ opt spaces
    ( [-+]? \d+ ) # Group 3: second number
    \s*           # opt spaces
    ( [\[\]] )    # Group 4: closing bracket
    \s* $         # opt spaces+ end at the end
    """, flags=re.VERBOSE)

Re: Python best practices

#12
This is an excellent overview of how Python is usually written by people who write (and read) a lot of it.

I hardly ever see coding conventions documents that do such a good job of capturing the popular conventions without inserting quirky personal preferences. I clicked through expecting something to point and laugh at, but was pleasantly surprised!

Re: Python best practices

#13
> if x is None: ... > if items is None: ...

None's truth value is false, so the above are equivalent to:

  if x: ...
  if items: ...
Empty sequences and mappings are also considered false, so you don't need to

  if len(items): ...
Instead, you should

  if items: ...
On a side note, does it annoy the hell out of anyone else that a sequence's length is len(foo) instead of foo.length() (or size, count, etc)?

Re: Python best practices

#14
post #7

Earlier quoted context omitted.

> Extraneous spaces annoy me to end. It is recommended in the "official" python style guide to surround the assignment operator with a single space: http://www.python.org/dev/peps/pep-0008/

And look how complicated they make it. Sometimes spaces, sometimes not, how confusing. And tell me that their "Use spaces around arithmetic operators" example doesn't make you want to puke: i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b) Who writes like that?? I very strongly disagree.

"Who writes like that??"

The authors of:

BitTorrent: https://develop.participatoryculture.org/trac/democracy/brow...

Django: http://code.djangoproject.com/browser/django/trunk/django/ut...

Pylons: http://pylonshq.com/project/pylonshq/browser/pylons/util.py

Twisted: http://twistedmatrix.com/trac/browser/trunk/twisted/python/u...

Re: Python best practices

#15

> if x is None: ... > if items is None: ... None's truth value is false, so the above are equivalent to: if x: ... if items: ... Empty sequences and mappings are also considered false, so you don't need to if len(items): ... Instead, you should if items: ... On a side note, does it annoy the hell out of anyone else that a sequence's length is len(foo) instead of foo.length() (or size, count, etc)?

There's a reason why "if x is None" is idiomatic when "if not x" is shorter:

  >>> x = ''
  >>> if x is None:
  ...   print 'x is None'
  ...
  >>> if not x:
  ...   print 'x is empty'
  ...
  x is empty

Re: Python best practices

#16
post #7

Earlier quoted context omitted.

> Extraneous spaces annoy me to end. It is recommended in the "official" python style guide to surround the assignment operator with a single space: http://www.python.org/dev/peps/pep-0008/

And look how complicated they make it. Sometimes spaces, sometimes not, how confusing. And tell me that their "Use spaces around arithmetic operators" example doesn't make you want to puke: i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b) Who writes like that?? I very strongly disagree.

I do all of these (even in interactive shell) except I have a weak spot for this sort of notation:

    i+=2

Re: Python best practices

#18
post #8
post #7

Earlier quoted context omitted.

And look how complicated they make it. Sometimes spaces, sometimes not, how confusing. And tell me that their "Use spaces around arithmetic operators" example doesn't make you want to puke: i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b) Who writes like that?? I very strongly disagree.

I do. I think it looks a lot better and more clear. It certainly looks a lot like what I would write down on paper, and that is one of the inherent qualities of Python in general.

The one place that I don't follow the guideline (unless it's the guideline and I just don't know it... it rarely comes up, so I haven't bothered to check) is on array indices. I do write:

  a[i+1]

Re: Python best practices

#19

> if x is None: ... > if items is None: ... None's truth value is false, so the above are equivalent to: if x: ... if items: ... Empty sequences and mappings are also considered false, so you don't need to if len(items): ... Instead, you should if items: ... On a side note, does it annoy the hell out of anyone else that a sequence's length is len(foo) instead of foo.length() (or size, count, etc)?

[deleted]
Post reply on HN