Live data from Hacker News

Python Language Features and Tricks

sahandsaba.com

51–60 of 86 posts

Re: Python Language Features and Tricks

#51
post #15
post #11

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.

A common theme running through named tuples, itertools and collections is Raymond Hettinger. He has also put together lots of tutorials and talks.

Re: Python Language Features and Tricks

#52
post #9
post #7

I 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

Cool like this,hope implement in php. No need to explode

Re: Python Language Features and Tricks

#54
post #46

Earlier 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

How do view patterns make patterns "first-class"? To me that means able to manipulate them as values, I don't see how view patterns allow that, they're just syntactic sugar for case expressions.

Re: Python Language Features and Tricks

#55

I'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 an unpleasant taste in your mouth at the sheer quantity of magic.

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

#56
post #28

Slice 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…

> I always forget that [1:3] is not all inclusive. It's actually just range from 1 to 2.

Just remember that for positive indices, len(slice(n,m)) == m - n

Re: Python Language Features and Tricks

#57

I 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?

So that flattening can also be written as:

    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

#58
post #55

I'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…

Been thinking about Python vs Ruby lately. I dread reading other people's Ruby code (eg. if there is a problem in a library I'm using) but I don't have that same dread reading Python code. Now if Python had blocks and RubyGems then I would never give Ruby a second look.

Re: Python Language Features and Tricks

#59

I'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 language has expanded a lot recently (2.x) series. It is no longer the language it once was. It is more advanced and not as svelte.

There's similar thing re eq/gte/etc for Python. Particulars escape me at the moment.

Re: Python Language Features and Tricks

#60
post #52
post #9

Earlier 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 has the list construct which is a little uglier, and has a couple of odd behaviors

    php > $t = array(1, 2);
    php > list($a, $b) = $t;
    php > echo "$a, $b";
    1, 2
Post reply on HN