r/Python • • Jun 23 '14

Writing better python code

https://gist.github.com/kracekumar/09a60ec75a4de19b346e
143 Upvotes

126 comments sorted by

View all comments

46

u/robin-gvx Jun 23 '14

I have problems with some of these.

  • Replacing a or b or c by any([a, b, c]). Is that really more Pythonic? If they'd been in an iterable already I'd say yes, but now not so much.

  • Catching KeyError just to raise another KeyError? Just let it bubble up.

  • I kinda hate namedtuple because it's such a hack, but maybe that's just me.

  • Also, I'd say the opposite of Pythonic code is not normal code. Un-Pythonic or unidiomatic, or maybe something about newbies.

1

u/[deleted] Jun 23 '14

Hack or not, named tuple is an amazing module for writing unit tests. Hardly an easier, lighter, and more self-documenting way to simulate a class in some specific state (so long as method calls aren't required).