LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl or Python ? (https://www.linuxquestions.org/questions/programming-9/perl-or-python-828635/)

Sergei Steshenko 08-26-2010 11:02 PM

Quote:

Originally Posted by ghostdog74 (Post 4079053)
There's more than one way to do it, but sometimes consistency is not a bad thing either (TIMTOWTDIBSCINABTE). You can follow TIMTOWTDI mantra all you want, that's your business and i don't care.

When you read a book on OO, you read on multiple constructors and polymorphic functions - both these categories essentially promote "there is more than one way to do it".

I am too lazy, but if to dig deeper, I'm sure I'll find in Python "there is more than one way to do it" in many more places.

ghostdog74 08-27-2010 01:04 AM

Quote:

Originally Posted by Sergei Steshenko (Post 4079066)
I am too lazy, but if to dig deeper, I'm sure I'll find in Python "there is more than one way to do it" in many more places.

The key word is "obvious". Please do "import this" in Python interpreter for Python's zen. There's a difference between "obvious" and an all out "there is more than one way to do it."

tell me which one to you is obvious in these Perl statements (and don't try to evade this question as well).

Code:

open FILE, '<', "foo.txt" or die "Can't open: $!";
open(FILE,"<foo.txt") or die "....";

How about the many different ways to write "if/else" construct?
Code:

if ( .... ) {
 ...
}

if (...)
{
 ...
}

if (
 ..
) {
  ...
}

which one is more obvious? In Python, there is only one (obvious and best) way to do it
Code:

if something :
  do something

And how about the many different ways to write for loops (including foreach etc etc) ? and the many different ways to ...... the list goes on


Sure you can find one of two Python's "more than one way to do it" , but when it comes to how many there are, Perl beats Python all the way.

MTK358 08-27-2010 09:30 AM

I suggest learning both. I haven't really tried Ruby much, maybe it's good. You can learn it too if you like.

And I agree with Sergei Steshenko's comment that you should read "Higher Order Perl" to really understand what it's all about.

David1357 08-27-2010 09:50 AM

Quote:

Originally Posted by ghostdog74 (Post 4078971)
There should be only just one obvious way to do it without beating about the bush.

You contradict both my own personal experience, statements made to me by other Perl users over the years, and statements made by the original author of Perl.

Quote:

Originally Posted by ghostdog74 (Post 4078971)
what do you mean by "doesn't know how to end a function"?

You misquoted me. I said
Quote:

Originally Posted by David1357 (Post 4078669)
Python ... has no way of indicating the end of a function.

If you don't understand the difference between your misquote and what I said, please do not comment any further.

ghostdog74 08-27-2010 10:19 AM

Quote:

Originally Posted by David1357 (Post 4079632)
You contradict both my own personal experience, statements made to me by other Perl users over the years, and statements made by the original author of Perl.

The statement I wrote is not directed at you. Its just an additional comment with regard to the wiki you linked about TIMTOWTDI.

Quote:

You misquoted me. I said
Maybe you are right. "no way of indicating" doesn't really mean "doesn't know how to" (or does it?)
don't give comments that are half truth and leave people guessing what it means next time.

Nevertheless, you still didn't explain the "no way of indicating end function" statement.

David1357 08-27-2010 10:43 AM

Quote:

Originally Posted by ghostdog74 (Post 4079664)
Nevertheless, you still didn't explain the "no way of indicating end function" statement.

If I write a C function, the brackets indicate the start and end of the function body. The same goes for a Perl subroutine. Python functions have a clearly defined beginning, but no clearly defined ending. For example, the following code is one Python function:
Code:

#!/usr/bin/python
def blah():
    print "Starting the function..."

    print "In the function..."






    print "Still in the function..."

blah()

That code produces
Code:

Starting the function...
In the function...
Still in the function...

Supposedly, the indentation rules are all I need to understand Python code. However, I find that the lack of any function-end indicator makes it hard for me to read Python.

ghostdog74 08-27-2010 11:48 AM

Quote:

Originally Posted by David1357 (Post 4079685)
However, I find that the lack of any function-end indicator makes it hard for me to read Python.

Python actually has an "end of function" indicator. That is the indentation stack containing value of 0.
I guess everybody is different. If you have used Python for years and still couldn't get used to its style, then its not for you.

Sergei Steshenko 08-27-2010 11:53 AM

Quote:

Originally Posted by ghostdog74 (Post 4079146)
...
How about the many different ways to write "if/else" construct?
Code:

if ( .... ) {
 ...
}

if (...)
{
 ...
}

if (
 ..
) {
  ...
}

...

Your criticism of Perl is obviously insufficient. There is more than one way to criticize Perl. In the context you forgot to mention Perl 'Switch' capability as yet another way to write 'if-then-else': http://perldoc.perl.org/Switch.html .

pr_deltoid 08-27-2010 03:20 PM

Quote:

Originally Posted by David1357 (Post 4079685)
Supposedly, the indentation rules are all I need to understand Python code. However, I find that the lack of any function-end indicator makes it hard for me to read Python.

I actually like the indentation and whitespace being important in Python. To me, it makes it look very neat and doesn't make it hard to read.

mattca 08-28-2010 12:05 PM

Quote:

Originally Posted by ghostdog74 (Post 4079146)
How about the many different ways to write "if/else" construct?
Code:

if ( .... ) {
 ...
}

if (...)
{
 ...
}

if (
 ..
) {
  ...
}


That is all the same syntax.. one way of doing an if statement, infinite ways of formatting it.

ghostdog74 08-28-2010 09:48 PM

Quote:

Originally Posted by mattca (Post 4080792)
That is all the same syntax.. one way of doing an if statement, infinite ways of formatting it.

that's why all these ways (TIMTOWTDI) of formatting lead to inconsistency. that's my point.

Sergei Steshenko 08-28-2010 11:08 PM

Quote:

Originally Posted by ghostdog74 (Post 4081127)
that's why all these ways (TIMTOWTDI) of formatting lead to inconsistency. that's my point.

And Python indentation are seen as "wacky" by some folks:

http://utcc.utoronto.ca/~cks/space/b...honIndentation
.

At all, if I understand it correctly, the indentation may be whatever number of spaces, just returning to previous level of indentation must have the same number of spaces, i.e.

Code:

  foo_indent_level
    bar_indent_level
    bar_indent_level
  foo_indent_level

.

Here is a thread about an indentation error:

http://bytes.com/topic/python/answer...nt-comphrehend

- though the thread is not about 'break', I find the code unreadable because of the way 'break' (line #36, #39) is indented. And, if I understand it correctly, there is more than one way to select indentation of 'break'.

ghostdog74 08-28-2010 11:48 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4081155)
At all, if I understand it correctly, the indentation may be whatever number of spaces, just returning to previous level of indentation must have the same number of spaces, i.e.

yes, that's right. It can be whatever number of spaces (standard is 4), but they must be aligned if not it will fail to run. Every Python programmer will have to align them properly to make their code run, which in turn the whole script will turn out nice and easy to read and maintain. And there is nothing really "wrong" in returning to the same number of spaces (indentation level). Example,

if you write it in Perl with good coding practice,

Code:

foo_indent_level1{
    bar_indent_level1
    bar_indent_level1
}
foo_indent_level2{
    bar_indent_level2
    bar_indent_level2
    if (foo) {
        bar_indent_level3
        bar_indent_level3
    }
}

versus one that is written like this

Code:

foo_indent_level2{
  bar_indent_level2
        bar_indent_level2
    if (foo) {        bar_indent_level3
        bar_indent_level3 }   
  }


Which one would you prefer ? A good Perl programmer would want to re-indent it nicely anyway.

Sergei Steshenko 08-29-2010 01:05 AM

Quote:

Originally Posted by ghostdog74 (Post 4081169)
...
Which one would you prefer ? A good Perl programmer would want to re-indent it nicely anyway.

Neither.

I indent this way:

Code:

if(...)
  {
  foreach my $x(@foo)
    {
    ....
    }
  }

.

Regarding "there is more than one way to do it" - just everything in programming after the Turing machine proves that there is more than one way to do it.

ghostdog74 08-29-2010 01:54 AM

Quote:

Originally Posted by Sergei Steshenko (Post 4081209)
Neither.

I indent this way:

that's hell ugly, but that's your business and i don't care.

Quote:

Regarding "there is more than one way to do it" - just everything in programming after the Turing machine proves that there is more than one way to do it.
I did not say i don't agree there's more than 1 way to do it, turing machine or not. I am only saying some ways "beat around the bush" (not obvious) to get to the solution, while some is straight to the point (and the best way).
I can't help it you don't see it that way, because that's your business and i can't be bothered.


All times are GMT -5. The time now is 04:30 AM.