News for hackers. Stuff that matters.
Re:Hidden features of Python by theli0nheart on Tuesday July 13, 2010 @07:06PM (#1513043)
My favorite feature (just released in Python 2.7, already in P3K) are dictionary comprehensions. E.g., you can now do:

    >>> {s: len(s) for s in ["one", "two", "three"]
    {"one": 3, "two": 3, "three": 5}
Re:Hidden features of Python by twism on Tuesday July 13, 2010 @08:29PM (#1513194)
In python land (Django) for the past couple of months and I've been loving it, just thought this was a good excuse to fire up slime. In clojure...

    (defn map-comprehension [f sequence]
          (reduce #(assoc %1 %2 (f %2)) {} sequence))
usage:

    user> (map-comprehension count ["one" "two" "three"])
    {"three" 5, "two" 3, "one" 3}
Re:Hidden features of Python by snprbob86 on Wednesday July 14, 2010 @03:17AM (#1513916)
Pre 2.7, you can just give the dict constructor a generator of tuples:

  >>> dict((s, len(s)) for s in ["one", "two", "three"])
  {'three': 5, 'two': 3, 'one': 3}
Re:Hidden features of Python by calibraxis on Wednesday July 14, 2010 @03:44AM (#1513961)
Thank you very much! Lack of simple dict-building had been bothering me.
Re:Hidden features of Python by omaranto on Wednesday July 14, 2010 @08:07PM (#1516397)
map-comprehension is a bad name for that function, since it doesn't give you comprehension-like syntax, instead it is just a function that behaves like map but produces a map instead of another sequence. I'd call it map-map.
Re:Hidden features of Python by mkramlich on Tuesday July 13, 2010 @07:19PM (#1513067)
more like new feature rather than hidden feature. but agreed, it's very cool
Re:Hidden features of Python by cma on Tuesday July 13, 2010 @07:29PM (#1513089)
No one mentioned Python's strong typing hidden feature:

    (True + True) == 2

(sorry, one of my few pet-peeves after it bit me one time)
Re:Hidden features of Python by mccutchen on Tuesday July 13, 2010 @11:44PM (#1513531)
This is just a side-effect of bool being a subclass of int and True and False having the values 1 and 0, respectively. It might seem weird or surprising, but it's not a problem with Python's type system.

    >>> issubclass(bool, int)
    True
    >>> isinstance(True, int)
    True
    >>> int(True)
    1
    >>> int(False)
    0
    >>> True == 1
    True
    >>> False == 0
    True
Re:Hidden features of Python by RodgerTheGreat on Tuesday July 13, 2010 @08:36PM (#1513203)
Seems fine just so long as (False + False) doesn't equal True.
Re:Hidden features of Python by cma on Tuesday July 13, 2010 @09:22PM (#1513284)
When is it ever desirable? I'm open to it having a use, but I don't see one. The only thing I've gotten from it is mismatched types that made it past some unit tests but blew up in the real world.
Re:Hidden features of Python by bobbyi on Wednesday July 14, 2010 @12:25AM (#1513595)
It's useful because it allows you to add together booleans to get a count of how many are true.

You can check if two out of three are true with

(bool1 + bool2 + bool3) >= 2

You can do

count_negatives = sum(number < 0 for number in numbers)

Re:Hidden features of Python by RodgerTheGreat on Tuesday July 13, 2010 @10:00PM (#1513347)
I was making a glib joke, but to answer your question this is the tradeoff of using a not-so-strongly-typed language. Weakly-typed languages often save you some typing (if you'll forgive the pun) and implicitly convert in ways that can make it difficult to prevent those mistakes.

It's just like Duck Typing- you're trading the need to explicitly specify an interface for the potential dangers of letting your interpreter "know what you mean".

If this is frequently a problem, well... there are a lot of strongly-typed languages out there waiting for you to give them a try.

Re:Hidden features of Python by masklinn on Wednesday July 14, 2010 @02:31AM (#1513835)
> I was making a glib joke, but to answer your question this is the tradeoff of using a not-so-strongly-typed language.

Not in this case. In this case it results from:

1. Python historically not having booleans and using 0 and 1 instead

2. As a result, `bool` inherits from `int` for compatibility reasons

    >>> issubclass(bool, int)
    True
with False mapping to 0 and True to 1 for compatibility purposes

    >>> int(False), int(True)
    (0, 1)
3. Add isn't overridden on bool

    >>> bool.__add__
    <slot wrapper '__add__' of 'int' objects>
therefore, when adding two booleans you're really adding two integers.

Most non-strongly-typed languages will not have the following behavior

    >>> "foo" + 3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: cannot concatenate 'str' and 'int' objects
Re:Hidden features of Python by masklinn on Wednesday July 14, 2010 @07:27AM (#1514253)
In fact, to expand on the latter part of my comment, "weak typing" is generally understood as implying implicit type coercions or conversions at runtime. PHP and Javascript famously convert between numbers and strings depending on a bunch of factors (and usually the wrong way leading to unreliable results).

In that sense, Python is a very strongly typed language: from the top of my head, the only example of implicit type conversion in Python is the promotion of `int` to `long`, which has been removed from 3.x (all integer types were unified). All other conversions have to be explicit as far as I remember. That makes Python more strongly (but less statically) typed than Java (see the very strange overloading of the `+` operator when any operand is a string, as well as autoboxing since 1.5) and C# (C# allows for implicit type conversions, including in userland, and the cast operator doubles up as an explicit type conversion operator which I find terrible)

Re:Hidden features of Python by eru on Tuesday July 13, 2010 @11:15PM (#1513491)
Python is fairly strongly typed in general. (At least compared to Perl and the like. Examples like True + True notwithstanding.) It is just also very dynamic.
Re:Hidden features of Python by oddthink on Wednesday July 14, 2010 @10:44AM (#1514687)
This is only tangentially related, but try looking up "Iverson brackets" and Knuth's "Two notes on notation". One argument there is that False - True = -1 makes sign(x) = [x > 0] - [x < 0], which is convenient. I don't know of a case where True + True = 2 helps, but maybe something involving dual sums would be relevant.
Re:Hidden features of Python by cma on Thursday July 15, 2010 @04:41PM (#1519104)
The Iverson Bracket would already exist in a python that didn't subclass bool from int; '[boolean_expression]' would be, in python notation: 'int(boolean_expression)'.

Sounds kinda like I'm making the same argument as Knuth in forcing '[]' over '()'.

Re:Hidden features of Python by jakevoytko on Tuesday July 13, 2010 @10:18PM (#1513378)
I used it in Code Golf to select a string from a list based on the truth of some conditions.

So no, it does not have a use :)

Re:Hidden features of Python by eru on Tuesday July 13, 2010 @11:14PM (#1513488)
In mathematics

  True + True == False
might be a common interpretation.
Re:Hidden features of Python by moron4hire on Tuesday July 13, 2010 @11:52PM (#1513546)
When? Because the + symbol is often used to represent boolean OR, so T + T == T.
Re:Hidden features of Python by praptak on Wednesday July 14, 2010 @03:28AM (#1513933)
Two element finite field.
Re:Hidden features of Python by eru on Wednesday July 14, 2010 @01:18PM (#1515274)
Indeed. There + becomes isomorphic to XOR and * to AND.
Re:Hidden features of Python by tkahn6 on Wednesday July 14, 2010 @02:23AM (#1513814)
Maybe with a one-bit half-adder?

     1
   + 1
   ----
     0
Re:Hidden features of Python by defun-fun on Wednesday July 14, 2010 @01:13AM (#1513662)
The greatest hidden feature of Python (for me) was its standard library. Its sheer size and completeness, I should add.

Yes, I have heard of "batteries included" and all, but after working with Python for a year I still felt that the size of its library has been "hidden" from general programming public.

I love Python.

Re:Hidden features of Python by cpprocket on Wednesday July 14, 2010 @01:16AM (#1513667)
You can add the number of Python implementation to your list of "hidden" features as well: IronPython, CPython, Jython, PyPy, unladen-swallow, Python3 - and all of them are of pretty high quality!
Re:Hidden features of Python by johnswamps on Tuesday July 13, 2010 @06:56PM (#1513020)
There's a bunch of "hidden features of X" on stackoverflow (http://stackoverflow.com/search?q=%22hidden%20features%22...) if you liked this one
Re:Hidden features of Python by kordless on Wednesday July 14, 2010 @03:21AM (#1513923)
python -m SimpleHTTPServer 8080
Re:Hidden features of Python by masklinn on Wednesday July 14, 2010 @07:28AM (#1514257)
Port is optional and defaults to 8000 too.
Re:Hidden features of Python by judofyr on Tuesday July 13, 2010 @07:06PM (#1513041)
Chaining comparison operators in Ruby: http://judofyr.net/posts/chained-comparisons.html
Re:Hidden features of Python by brlewis on Wednesday July 14, 2010 @11:11AM (#1514794)
I feel validated that the chaining comparison operators feature is #1 in terms of votes (303 vs #2 at 249 as I write this). Nobody else seems to think it worth mentioning, but chaining comparison operators is a feature I'm really fond of in Lisp/Scheme: http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node124.html
Re:Hidden features of Python by omaranto on Wednesday July 14, 2010 @08:09PM (#1516406)
I wouldn't say Lisp has chaining of comparisons, only chaining of identical comparisons: a < b < c --> (< a b c), but a < b <= c --> (and (< a b) (<= b c))
Re:Hidden features of Python by brlewis on Thursday July 15, 2010 @08:10AM (#1517546)
Still, chaining identical comparisons is a feature I think is cool.
Re:Hidden features of Python by tomazmuraus on Wednesday July 14, 2010 @11:20AM (#1514821)
python -m smtpd -n -c DebuggingServer localhost:2525

Runs a local SMTP server on port 2525. Very useful for debugging.

Re:Hidden features of Python by RyanMcGreal on Wednesday July 14, 2010 @08:26AM (#1514356)
>Wrap an iterable with enumerate and it will yield the item along with its index.

Now that's a feature I wish I knew about sooner. I've been using:

    for item in a:
	print a.index(item), item
which has always felt kludgy.
Re:Hidden features of Python by eru on Wednesday July 14, 2010 @03:10PM (#1515612)
Yes, enumerate is useful for that. But even if you did not now about it you could have used count from itertools to get the same effect:

  import itertools
  for (index, item) in zip(itertools.count(0), "hallo"):
      print index, item
Itertools is worth checking out for other goodies, too.
Re:Hidden features of Python by infinite8s on Thursday July 15, 2010 @05:39PM (#1519311)
Before enumerate, the standard idiom for this was:

    for i in range(len(a)):
        print i, a[i]
Re:Hidden features of Python by petdog on Wednesday July 14, 2010 @10:05AM (#1514576)
Seriously? You must enjoy your O(n^2).
Re:Hidden features of Python by RyanMcGreal on Wednesday July 14, 2010 @10:28AM (#1514635)
The sets I'm working on tend not to tend toward infinity.
Re:Hidden features of Python by eru on Wednesday July 14, 2010 @03:12PM (#1515620)
Yes. But the approach with index only works for actual sets. And if you have actual sets, you might as well use Python's built-in sets.
Re:Hidden features of Python by petdog on Wednesday July 14, 2010 @10:17AM (#1514606)
So by "hidden features" they mean "can be found in the first pages of any tutorial"

(Ok, I didn't know about re.DEBUG)

EDIT: I'm not claiming this link is worthless. But I'd call it "great features of python" or simply "features of python"