Earlier quoted context omitted.
'Lazy lists' are called generators in Python. Just use (...) Instead of [...] in your example.
I'm aware of generator expressions, but the fact that list comprehensions are not lazy can trip people if they are used to other languages with this feature, since they are usually lazy.
Python Language Features and Tricks
61–70 of 86 posts
Re: Python Language Features and Tricks
#62it's amazing how much work and effort almost any of this examples would take to implement in C
Re: Python Language Features and Tricks
#63I've been using Python and Ruby on and off for a couple years (largely because I haven't found the need to use it seriously day job or side projects). One thing that strikes odd for me is how people describe Python/Ruby are way more readable than Java. I felt that Python, while more readable than Ruby (because Python uses less symbols), still contain more nifty tricks compare to Java. It's true that the resulting cod…
The comparison functions are there to address the comparison for equality of things that don't have an order. So in some sense, they are more explicit (a minute with the docs explains that implementations only need to directly define 2 of them). -1,0,1 style comparisons still exist in Python 2.7, they were dropped in 3.0. Probably one of those things that could have gone either way. I think comparisons about readabil…
Re: Python Language Features and Tricks
#64I've been using Python and Ruby on and off for a couple years (largely because I haven't found the need to use it seriously day job or side projects). One thing that strikes odd for me is how people describe Python/Ruby are way more readable than Java. I felt that Python, while more readable than Ruby (because Python uses less symbols), still contain more nifty tricks compare to Java. It's true that the resulting cod…
In isolated offline code, I think Ruby approaches the unreadable - but only because DSLs are fashionable, and you have to understand the library involved to understand the magic that's going on behind the scenes. With sufficient online searching, you can understand things at a surface level, and with enough time with a debugger you can appreciate all the mechanics that are happening, but IMHO it still leaves you with…
By symbol I mean the use of tilda, pound sign, ampersand colon.
Seems to me that it's more of personal preference than anything else and this is why for me personally, Ruby is pretty close to Perl.
Somehow I prefer verbose than too terse. I prefer to read the code in words than in mix of words and symbols.
Re: Python Language Features and Tricks
#65Coming from a long history of languages like BASIC and Pascal, I will bookmark this tutorial. It seems to open up a lot of interesting Python features that were, quite frankly, not always easy to understand when described in plain text, but now seem pretty simple when presented as examples. I'll also think about the "collection of simple examples" next time I want to document something.
I just bookmark the HN discussion. Not only does that retain a link to the tutorial, but it also retains a link to a discussion that usually adds value to the tutorial.
I'm assuming that HN discussions remain available for many years. Anyone know for sure if that is/isn't true?
Re: Python Language Features and Tricks
#66Coming from a long history of languages like BASIC and Pascal, I will bookmark this tutorial. It seems to open up a lot of interesting Python features that were, quite frankly, not always easy to understand when described in plain text, but now seem pretty simple when presented as examples. I'll also think about the "collection of simple examples" next time I want to document something.
I used to bookmark tutorials like this, but now ... I just bookmark the HN discussion. Not only does that retain a link to the tutorial, but it also retains a link to a discussion that usually adds value to the tutorial. I'm assuming that HN discussions remain available for many years. Anyone know for sure if that is/isn't true?
Re: Python Language Features and Tricks
#67I have two questions. 1. I'm unfamiliar with the term 'unpacking'. Is it any different from pattern matching in, say, Haskell (but perhaps not as feature-rich)? 2. Aren't slices pretty much a staple in Python? I didn't think using them was considered a 'trick'.
Unpacking is a limited form of what is called destructuring in other languages like Clojure. I would say that, in terms of feature-richness: unpacking < destructuring < pattern matching.
a,(b,c),d = [1,[2,3],4]Re: Python Language Features and Tricks
#68Earlier quoted context omitted.
Same here, learned something new today.
A common theme running through named tuples, itertools and collections is Raymond Hettinger. He has also put together lots of tutorials and talks.
Really enjoy his style as well :)
Re: Python Language Features and Tricks
#69Why are there so many negative comments? Maybe those posters are vastly underestimating how many people that just start out read HN. I think it's a pretty good post to read after something like "X in Y minutes - Python" to get a very quick grasp of what the language is like. I'm also not ashamed to say that despite having written quite a few LOC of Python I wasn't aware of named slices for some reason and I think the…
I've been using python since the 90s, and I learned a few things from this post.
Re: Python Language Features and Tricks
#70Earlier quoted context omitted.
'Lazy lists' are called generators in Python. Just use (...) Instead of [...] in your example.
I'm aware of generator expressions, but the fact that list comprehensions are not lazy can trip people if they are used to other languages with this feature, since they are usually lazy.
I think it's a lack of naming convention - the word "list" on its own means "strict list" in some languages and "lazy list" in others.