News for hackers. Stuff that matters.
Re:First Impressions of Python from a Java Developer by nivertech on Monday November 08, 2010 @01:43PM (#1883440)
One thing I dislike in Python's syntax is ":" at the end of some composite statements, i.e. "if X:" or "else:". "else" is reserved word, why do I need to append ":"?

Also how to do one-liners? ;)

Re:First Impressions of Python from a Java Developer by xi on Monday November 08, 2010 @02:14PM (#1883565)
Just like semantic indentation, ":" is ABC's heritage. It was discovered that newbie programmers comprehend block operations better when they end with ":".

http://python-history.blogspot.com/2009/02/early-language-de...

Re:First Impressions of Python from a Java Developer by nivertech on Monday November 08, 2010 @04:15PM (#1884042)
back in time, there was several schools for language syntax design for composite statements:

1. "Open" statements (Pascal):

  IF x 
    THEN s1
    ELSE s2;

  IF x 
    THEN
      BEGIN
        s1;
        s2
      END
    ELSE
      BEGIN
        s3;
        s4
      END

  WHILE c DO s;

  WHILE c DO
  BEGIN
     s1;
     s2
  END
The problem with this design was need to use blocks (BEGIN ... END) and complex rules in case of nesting.

2. "Closed" statements (Algol 68)

  IF x 
    THEN 
       s1;
       s2
    ELSE 
       s3;
       s4
  FI // END IF

  WHILE c DO
     s1;
     s2
  OD // END WHILE
You can see new kind of parenthesis IF-FI, DO-OD, etc.

I more like "closed" syntax. But C syntax with it's brackets blocks "{...}" instead of "BEGIN...END" made this less an issue.

Re:First Impressions of Python from a Java Developer by ronnix on Monday November 08, 2010 @01:55PM (#1883486)
You can have a one-line "if" statement [1]:

  if a == 42: print "Yes" ; do_stuff()
It's true that you cannot have a "else" statement on the same line, however.

But in some cases, you could use a conditional expression instead [2]:

  x = f() if a == 42 else g()
[1] http://docs.python.org/reference/compound_stmts.html

[2] http://docs.python.org/reference/expressions.html#conditiona...

Re:First Impressions of Python from a Java Developer by nivertech on Monday November 08, 2010 @02:03PM (#1883517)
in this case plain old C syntax is much more readable:

x = (a==42) ? f() : g();

Re:First Impressions of Python from a Java Developer by pyre on Monday November 08, 2010 @03:52PM (#1883965)
I think that it's much more readable due to familiarity more than anything else. The C syntax for the ternary operator was the one thing that it took me the longest time to get used to when learning to program. I usually just ignored it because it was too hard to remember what it did. That said, I can easily read it now, but I still feel that the Python syntax is more understandable.

Which is more readable:

  > condition ? value1 : value2
or

  > value1 if condition else value2
Maybe the latter is just more readable to me because I'm used to reading Perl all day long that looks like:

  > die unless condition;
  > return true if condition;
  > return false
  >     if condition1 && condition2 && condition3;
That said, I'm also used to reading:

  > return condition1   ? value1
  >        : condition2 ? value2
  >        : condition3 ? value3
  >                     : value4;
and I still find the Python ternary operator more readable:

  > return value1 if condition1 else
  >        value2 if condition2 else
  >        value3 if condition3 else
  >        value4
Re:First Impressions of Python from a Java Developer by j_baker on Monday November 08, 2010 @03:58PM (#1883982)
Personally, I still have to think about what the ternary operator means every time I see it. It's just so nonobvious.
Re:First Impressions of Python from a Java Developer by orangecat on Monday November 08, 2010 @03:09PM (#1883780)
In this case I think the PEP8 spacing guidelines are counterproductive:

    f() if a==42 else g()
looks much more readable to me than

    f() if a == 42 else g()
Re:First Impressions of Python from a Java Developer by ronnix on Monday November 08, 2010 @05:44PM (#1884367)
You can also write it this way, which is more similar to C:

  x = (a==42) and f() or g()
Re:First Impressions of Python from a Java Developer by j_baker on Monday November 08, 2010 @03:05PM (#1883767)
I think the FAQ answers this question nicely: http://docs.python.org/faq/design.html#why-are-colons-requir...
Re:First Impressions of Python from a Java Developer by juanefren on Monday November 08, 2010 @01:58PM (#1883502)
As a non expert developer, I hate to see one-liners (using lambda expressions) when I am inspecting open source code.

So I would prefer they were not possible :)

Re:First Impressions of Python from a Java Developer by xiongchiamiov on Monday November 08, 2010 @07:00PM (#1884613)
While one-liners are almost always bad, the lack of non-crippled anonymous functions is the one thing that really bothers me about Python. They don't /have/ to be on one line; see Ruby's each blocks, for instance.
Re:First Impressions of Python from a Java Developer by riobard on Monday November 08, 2010 @07:21PM (#1884700)
Every once in a while this will be brought up and debated to death...

See my last attempt in Feb 2009: http://mail.python.org/pipermail/python-ideas/2009-February/...

Re:First Impressions of Python from a Java Developer by reinhardt on Monday November 08, 2010 @07:15PM (#1884678)
If you like Python but not the ":", check out the Cobra language: http://cobra-language.com/samples/WordCount/. FWIW I do like the ":", reminds me of a TODO list; in fact my plain text notes tend to look like Python ;)
Re:First Impressions of Python from a Java Developer by jbail on Monday November 08, 2010 @01:49PM (#1883463)
You can do one liners with lambda functions in Python. e.g:

lambda x: x*2

http://diveintopython.org/power_of_introspection/lambda_func...

Re:First Impressions of Python from a Java Developer by koenigdavidmj on Monday November 08, 2010 @02:00PM (#1883509)
The colon is there to make it easier on editors that want to do auto-indentation. Their logic can simply be that if the previous line ends with a colon, then add a new indentation level.
Re:First Impressions of Python from a Java Developer by nivertech on Monday November 08, 2010 @02:11PM (#1883549)
Now we supposed to help editors? ;) I thought it should be otherwise... editors supposed to be helping programmers.

Programmers can learn syntax. Regular humans can't. Both Python and Ruby failed to provide a simple way of building internal DSLs. In Python humans need to be specific about indentation and ":" and in Ruby they need to remember all these stupid sigils.

Re:First Impressions of Python from a Java Developer by erikano on Monday November 08, 2010 @01:46PM (#1883449)
lambda functions are pretty neat for one-liners.