5 ms·
Python Generator Tricks
- RiderOfGiraffes 16y agoReading this item : http://news.ycombinator.com/item?id=1738906 http://news.ycombinator.com/item?id=1738906 : in which generators are discussed in comparison with the usual operations from functional programming languages, I was reminded of this article. Hence the submission.
- metachris 16y agoInteresting post -- I've come to appreciate linuxgazette as a pretty good resource. As a minor sidenote, I personally think 4 spaces for indentation makes the code more readable.
- bmelton 16y agoI do almost all of my code editing in regular text editors, Vi or Notepad, usually. 4 spaces is impossible for me. Why do you feel that 4 spaces is better than 5, which is usually the number of spaces represented by \t?
- silvajoao 16y agoIn my particular instance of the Universe, \t is usually represented by 8 spaces. I still prefer 4 though.
- bmelton 16y agoAs it happens, I can't count. \t IS 8, though hopefully there's some other reason I got downvoted to -4 than that. Still though, my question remains. Is there a particular reason you prefer 4? Does it affect your workflow in any way or does it just 'look better' in the abstract? I'm curious.
- silvajoao 16y agoI use a single tab for indentation, and too much exposure to Java and the Eclipse default indentation size (4 spaces per tab) made me used to it. I also find it easier to look at python code indented by 4 spaces per tab. Since I also tend to limit line length to 80 chars, 8-spaces tabs would quickly use up precious space. Linus disagrees: http://lxr.linux.no/linux+v2.6.35.7/Documentation/CodingStyle#L18 http://lxr.linux.no/linux+v2.6.35.7/Documentation/CodingStyl...
- metachris 16y agoI don't exactly know why, I just find it easier read with 4 spaces. Maybe a subconscious issue about the eyes having to move more with 8 spaces.
- beambot 16y agoThe official "python style guide" dictates using 4 spaces for indentation (unless old / legacy code): http://www.python.org/dev/peps/pep-0008/ http://www.python.org/dev/peps/pep-0008/
- jgresula 16y agoHere is a great practical intro to generators: Generator Tricks for Systems Programmers - http://www.dabeaz.com/generators-uk/GeneratorsUK.pdf http://www.dabeaz.com/generators-uk/GeneratorsUK.pdf
- viraptor 16y agoHere's also an interesting blog post from the creator of Diesel - http://blog.jamwt.com/2010/08/04/generators-and-back-again/ http://blog.jamwt.com/2010/08/04/generators-and-back-again/ It's about using generators for trivial task scheduling.
- praptak 16y agoPython's own @contextmanager decorator ( http://docs.python.org/library/contextlib.html#contextlib.contextmanager http://docs.python.org/library/contextlib.html#contextlib.co... ) uses generators in quite a clever way. You can grok quite a lot about Python generators by trying to figure out what @contextmanager actually does under the hood.
- bravura 16y agoPlease allow me to pickle simple generators. Otherwise, I have to go through all sorts of contortions: http://metaoptimize.com/blog/2009/12/22/why-cant-you-pickle-generators-in-python-workaround-pattern-for-saving-training-state/
- deleted 16y ago[deleted]
- deleted 16y ago[deleted]
- adambyrtek 16y agoThe article is from 2004, so it doesn't mention modern features like generator expressions, context managers or co-routines (PEP 342). I suspect it was upvoted so much mostly because of the SICP reference.
- gkelly 16y agoIt's also importing from __future__, which isn't necessary anymore.