MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/28utww/writing_better_python_code/ciex7ls/?context=9999
r/Python • u/kracekumar • Jun 23 '14
126 comments sorted by
View all comments
46
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.
a or b or c
any([a, b, c])
Catching KeyError just to raise another KeyError? Just let it bubble up.
KeyError
I kinda hate namedtuple because it's such a hack, but maybe that's just me.
namedtuple
Also, I'd say the opposite of Pythonic code is not normal code. Un-Pythonic or unidiomatic, or maybe something about newbies.
7 u/pstch Jun 23 '14 Could you expand on why "namedtuple" is such a hack ? 9 u/robin-gvx Jun 23 '14 Because it's implemented as piecing together a string that contains a class statement and compiling that to obtain the tuple subclass (see source). 4 u/[deleted] Jun 23 '14 Goofy implementation, doesn't need to be written that way 1 u/robin-gvx Jun 23 '14 Theoretically, it could just be rewritten as a call to type, but maybe there are practical issues? 2 u/[deleted] Jun 23 '14 The point is that it's not really a hack. Their implementation may be "hackish" but it doesn't depend on a hack to work.
7
Could you expand on why "namedtuple" is such a hack ?
9 u/robin-gvx Jun 23 '14 Because it's implemented as piecing together a string that contains a class statement and compiling that to obtain the tuple subclass (see source). 4 u/[deleted] Jun 23 '14 Goofy implementation, doesn't need to be written that way 1 u/robin-gvx Jun 23 '14 Theoretically, it could just be rewritten as a call to type, but maybe there are practical issues? 2 u/[deleted] Jun 23 '14 The point is that it's not really a hack. Their implementation may be "hackish" but it doesn't depend on a hack to work.
9
Because it's implemented as piecing together a string that contains a class statement and compiling that to obtain the tuple subclass (see source).
4 u/[deleted] Jun 23 '14 Goofy implementation, doesn't need to be written that way 1 u/robin-gvx Jun 23 '14 Theoretically, it could just be rewritten as a call to type, but maybe there are practical issues? 2 u/[deleted] Jun 23 '14 The point is that it's not really a hack. Their implementation may be "hackish" but it doesn't depend on a hack to work.
4
Goofy implementation, doesn't need to be written that way
1 u/robin-gvx Jun 23 '14 Theoretically, it could just be rewritten as a call to type, but maybe there are practical issues? 2 u/[deleted] Jun 23 '14 The point is that it's not really a hack. Their implementation may be "hackish" but it doesn't depend on a hack to work.
1
Theoretically, it could just be rewritten as a call to type, but maybe there are practical issues?
type
2 u/[deleted] Jun 23 '14 The point is that it's not really a hack. Their implementation may be "hackish" but it doesn't depend on a hack to work.
2
The point is that it's not really a hack. Their implementation may be "hackish" but it doesn't depend on a hack to work.
46
u/robin-gvx Jun 23 '14
I have problems with some of these.
Replacing
a or b or cbyany([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
KeyErrorjust to raise anotherKeyError? Just let it bubble up.I kinda hate
namedtuplebecause 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.