How clean/elegant is Python's syntax?

Chris Angelico rosuav at gmail.com
Thu May 30 14:22:50 EDT 2013


On Fri, May 31, 2013 at 3:59 AM, rusi <rustompmody at gmail.com> wrote:
> On May 30, 10:28 pm, Chris Angelico <ros... at gmail.com> wrote:
>> On Fri, May 31, 2013 at 3:12 AM, rusi <rustompm... at gmail.com> wrote:
>> > You associate the primal (f)act of thinking about programming with
>> > *doing* the generating.
>> > By contrast the functional programmer thinks about what *is* the
>> > result.
>>
>> I wish you'd explain that to my boss :) He often has trouble
>> understanding why sometimes I put two syntactic statements on one
>> line, such as:
>>
>> for (int i=0;i<nfoo;++i) if (foo[i].marker)
>> {
>>     //do something with foo[i]
>>
>> }
>>
>> In Python, that would probably be done with a list comprehension or
>> some other form of filtered iteration, and is to my mind a single
>> operation - "iterate over all the marked foo" is just as much a valid
>> loop header as "iterate over all the foo". This is a simple example,
>> and what you say about thinking about what *is* the result doesn't
>> really translate well into a C++ example, but the broader concept
>> applies: there's a difference between code as the compiler/interpreter
>> sees it and code as the programmer sees it, and there is not always a
>> 1:1 correspondence of statements.
>>
>> ChrisA
>
> I had a blog post about line-length in programs
> http://blog.languager.org/2012/10/layout-imperative-in-functional.html
>
> followed by an interesting discussion on the haskell mailing list
> http://groups.google.com/group/haskell-cafe/browse_thread/thread/f146ec7753c5db56/09eb73b1efe79fec
>
> The comment by Alexander Solla was insightful and is probably what you
> are saying.
>
> [Probably!! I am not sure what you are saying!]

Unfortunately a lot of your code specifics don't mean much to me
because I don't speak Haskell, but you are making several similar
points. A line of code should not be defined by the language's syntax,
but by the programmer's intention. A Python example might be:

for x in filter(lambda x: x%5 and x%6,range(40)):
    # do something with the numbers that don't count by 5 or 6

Stupid example, but it still puts the conditional inside the loop
header. I'm sure you can come up with a more useful case!

ChrisA



More information about the Python-list mailing list