Live data from Hacker News

Python Language Features and Tricks

sahandsaba.com

61–70 of 86 posts

Re: Python Language Features and Tricks

#61
post #35

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.

I don't agree, people are more likely to come from python that to python from a language that has lazy list comprehensions. But regardless, using a language requires that you learn it and I doubt many people think list comp is lazy. Pythonic programming obeys explicit over implicit, which which if you want a lazy version you explicitly use a lazy version.

Re: Python Language Features and Tricks

#63

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

Fair point and I learned early on during my college years not to import habit from one language to the other one. Mainly because "idiomatic" tend to depend on the internal implementation of the language.

Re: Python Language Features and Tricks

#64
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…

But that's the thing y'know... It's not just about DSL but it's also Ruby's syntax that include lots of symbols (of course DSL made the learning curve increase as well).

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

#65

Coming 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

#66

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

I hope so.

Re: Python Language Features and Tricks

#67
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'.

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.

I don't know Clojure, but I do know Python, and I'd like to say that unpacking is more flexible than you may realize. For example, you can do this:

  a,(b,c),d = [1,[2,3],4]

Re: Python Language Features and Tricks

#68
post #15

Earlier 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.

Great suggestion. Just searched for him on youtube and a couple of excellent looking talks popped up. Halfway through and this one seems great so far: https://www.youtube.com/watch?v=OSGv2VnC0go

Really enjoy his style as well :)

Re: Python Language Features and Tricks

#69
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…

I've been using python since the 90s, and I learned a few things from this post.

Right after this string of replies, I was expecting Guido van Rossum would be next, saying he created Python and even he learnt something new...

Re: Python Language Features and Tricks

#70
post #35

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.

Reading through http://en.wikipedia.org/wiki/List_comprehension, it seems that there are roughly equal numbers of languages where "list comprehension" produces strict list as languages where it produces lazy lists.

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.

Post reply on HN