News for hackers. Stuff that matters.
Re:Python Facts by kisielk on Friday November 25, 2011 @12:36PM (#3277614)
A bit of a nitpick, but it would be nice if the facts were PEP8 compliant so they encouraged good code style as well as showing off Python features.
Re:Python Facts by cbo on Friday November 25, 2011 @04:38PM (#3278317)
Not just PEP8-compliant. Some of these are just plain bad.

The first one I was greeted with when I opened the site was:

> print eval('+'.join(map(str, [1, 1])))

If I ever saw this in production code, I would lose my mind.

It's the kind of one-liner I'd expect to find in an obfuscated code competition. It's not intuitively understood, shows a lack of basic Python functionality (the builtin sum() function), and most of all, it's SLOW. As in, 54 times slower than a simple sum -- tested on xrange(1,10) -- on my development Macbook Pro.

That said, clicking through some other examples, there are a lot of great Python examples on this site. Just beware of some of them, and always make sure you test new code yourself.

Re:Python Facts by gbog on Friday November 25, 2011 @08:03PM (#3278755)
Slowness is not the worst, eval is evil, and should be forbidden, that's all.
Re:Python Facts by shenberg on Friday November 25, 2011 @07:22PM (#3278675)
I'm pretty sure that one-liner is a joke...
Re:Python Facts by ronreiter on Friday November 25, 2011 @01:51PM (#3277869)
i'm doing my best to moderate them, but you know how it works...
Re:Python Facts by drx on Friday November 25, 2011 @02:05PM (#3277912)
Try https://github.com/jcrocholl/pep8

You could show the output of this to the submitter (somewhat bad -- barrier to submission) or flag them yourself so you can correct them occasionally.

Examples:

  $ pep8 optparse.py
  optparse.py:69:11: E401 multiple imports on one line
  optparse.py:77:1: E302 expected 2 blank lines, found 1
  optparse.py:88:5: E301 expected 1 blank line, found 0


  $ pep8 --show-source --show-pep8 testsuite/E111.py
  testsuite/E111.py:2:3: E111 indentation is not a multiple of four
    print x
    ^
      Use 4 spaces per indentation level.

      For really old code that you don't want to mess up, you can
      continue to use 8-space tabs.
Re:Python Facts by mapleoin on Friday November 25, 2011 @02:06PM (#3277916)
So then you could add something like a 'report' button to allow users to say what they think is mistaken.
Re:Python Facts by shenberg on Friday November 25, 2011 @04:25PM (#3278276)
I'm not sure I agree - if it helps get the point of the snippet across more succinctly, it's worth breaking the guidelines.