Why 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…
Same here, learned something new today.
Python Language Features and Tricks
51–60 of 86 posts
Re: Python Language Features and Tricks
#52I 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'.
Tuple unpacking: t = (1, 2) a, b = t # a == 1 and b == 2 Using slices is normal with lists
Re: Python Language Features and Tricks
#53This is awesome, I've been programming python for about 8 years now and a lot of these still surprised me.
Re: Python Language Features and Tricks
#54Earlier quoted context omitted.
I did a little googling, but am finding it difficult to find good clear information - do you have any articles where I can read about first-class patterns?
https://ghc.haskell.org/trac/ghc/wiki/ViewPatterns That's the haskell extension. To see a very nice use of them, check out this paper (pdf) http://strictlypositive.org/CJ.pdf
Re: Python Language Features and Tricks
#55I'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…
I do prefer Ruby to Python though. I much prefer monadic enumerators combined with lambdas to list comprehensions, even though these tend to be eager in Ruby but have the option of being lazy in Python. Itertools helps, but is deeply hampered by Python's overly verbose lambda syntax.
Re: Python Language Features and Tricks
#56Slice has always been a painful adventure for me. I always forget that [1:3] is not all inclusive. It's actually just range from 1 to 2. I believe in 2.7 zip is still returning a list rather than an iterator ( izip in Python 2, zip in Python 3+). Another unappreciated stdlib is definitely functools . mock is also another awesome stdlib. functools , collections and itertools are definitely useful to make things faster…
Just remember that for positive indices, len(slice(n,m)) == m - n
Re: Python Language Features and Tricks
#57I think this is great, I've been doing Python for a while and I knew many of the features but I also learned a few new ones. I don't understand how this one to flatten lists works: a = [[1, 2], [3, 4], [5, 6]] [x for l in a for x in l] Can somebody explain what the order of operations is here and what the variables refer to in the various stages of evaluation?
x = [[1,2], [3,4], [5,6]]
[x for x in x for x in x]
More perversely, we can also flatten with my invention: list(None for x in x if (yield from x) and False) # Python >=3.3
More reasonably, of course, list(itertools.chain.from_iterable(x))Re: Python Language Features and Tricks
#58I'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…
Re: Python Language Features and Tricks
#59I'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…
There's similar thing re eq/gte/etc for Python. Particulars escape me at the moment.
Re: Python Language Features and Tricks
#60Earlier quoted context omitted.
Tuple unpacking: t = (1, 2) a, b = t # a == 1 and b == 2 Using slices is normal with lists
Cool like this,hope implement in php. No need to explode
php > $t = array(1, 2);
php > list($a, $b) = $t;
php > echo "$a, $b";
1, 2