6 ms·
Writing Idiomatic Python Book
- noir_lord 13y agoI bought it, I write quite a lot of Python (I use it for automation, deployment etc) but I sometimes feel like I'm fighting the language. Also the "don't write this" looked awfully familiar.
- julezy1 13y agoThis guy wrote a great article a while back [1], great to see him turning this into a book. I wish this kind of thing would go into the core python documentation though. So new Python programmers learn to 'think' python. --- [1] http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-p...
- avn2109 13y agoHave read this myself, and can confirm it's pretty solid stuff. Also, the writing style has an admirably high information density. He cuts to the chase.
- jey 13y agoWhen is it a good idea to use @property? It's tempting to apply to almost every 0-ary method, even if it computes something expensive that isn't not worth memoizing and tying to self's lifetime.
- GuiA 13y agoIf it is a 0-ary method AND does not mutate the state of its instance.
- benjamincburns 13y ago... and if it's not likely to cause performance issues.
- Ysx 13y agoAnd if you're not going to leak an AttributeError from any code in your method, else hasattr will return False.
- benjamincburns 13y agoCan't afford the book? Not a problem If your financial situation doesn't afford you the opportunity to purchase the book, email me at jeff@jeffknupp.com and I'll happily send you a free copy. Just be sure to note in your email which version you'd like. See, now that's just classy. I was on the fence about whether or not to buy before seeing that and somehow it tipped the balance for me. No idea what it's doing on the whole for your conversion rate, but it definitely helped here!
- Orangeair 13y agoIn the example he gives, wouldn't it be more simple (if not idiomatic) to write print(index, element) instead of print ('{} {}'.format(index, element)) ? Why use string formatting for something so simple, especially when Python inserts spaces in between multiple items automatically?
- shin_lao 13y agoI think it's because you're likely to change formatting as your code evolve.
- avelis 13y agoI agree on both points. Therefore, when the time comes, change it. No need to over design if unnecessary. The reason I agree to using .format is because it is more expressive in describing the output vs inference in using print.
- d0mine 13y agoYou could use .format() to support both Python 2 and 3 from the same source
- middus 13y agoThis marketing site fails to answer the most important question: What's the value proposition? Why should I care writing "Pythonic" code?
- andybak 13y agoI think that might fall under "If you need to ask, you're not the intended audience".
- elbear 13y agoHe expects you to already be convinced that writing idiomatic Python is what you want to do. If you're not, here are some arguments: 1. The idioms are generally the most elegant way of using the language, so if you use them, it means that you will use the language in the most efficient manner ("with" statement for file handling is one example); 2. It's easier for other people to work with your code; 3. You feel good knowing you're doing the right thing.
- Bootvis 13y agoGood points, I'd like to add: 4. Idiomatic code receives more attention from the Python core developers and might become faster in the future (for free!).
- nutate 13y agoI've had these two books for a while. He updates them rather regularly and they contain exactly what the author states. Python isn't supposed to have TIMTOWTDI, but when you start getting in the weeds you figure out bad ways to do common things. This book can help you recover from bad habits.
- FireBeyond 13y agoI’m confused... I bought this in January 2013. This doesn’t mention an update... is it that it’s now a book that’s new?