Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 7th, 2006, 1:16 AM   #11
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Jessehk
I think some of the complicated list comprehensions are unreadable unless you've written them yourself.

Specifically the stuff I've seen Sane write...

Getting back on topic, this is my impression of Pythonic. To make a reverse() function that will return either a reversed string, tuple, or list, I wrote this:

(snip long reversefunc)

Whereas Arevos' much more elegant, and "pythonic" code looked like this:
(snip shorter reversefunc)

I think the differences are obvious :p
YMMV but I find the following more Pythonic ;-)
def reverse(x):
    return x[::-1]

for elem in ([1,2,3], (1,2,3), "123"):
    print reverse(elem)
For me, Pythonicity is about elegance and clarity of structure and intent. Pythonic code is "pretty", as brief as it can be but not at the expense of readability, and written using Python idioms. (This is really just good coding practice for any coding language)

-T.
hydroxide is offline   Reply With Quote
Old Apr 7th, 2006, 1:28 AM   #12
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Master
I don't really find it readable as you think. I do understand what the code does. But the formatting is not all that great. it is better to use braclet or begin and end like ruby does. Note i am not try to say that python is not good. But the way things are written in it makes it seems as if there is there a beginning but no end such
def blah(str):
  print(str)
  while(true):
    print(str)
    if(str == Nothing):
        break
def dosomething():
   print("something")
I don't really see that as been readable
Neither do I, but then you haven't really written Python - it's like dissing Ruby for using $_, etc. The following is better (I amended the logic of your code slightly so that it would actually halt, and there would be no duplicate printing):
def blah(mystr):
    while mystr:
        print mystr
        mystr = mystr[:-1]

def dosomething():
   print "something"
Personally I loathe ruby's "end" <shrug>. I believe that emacs pymode uses #end for enforcing indentation, if you really want it.

-T.
hydroxide is offline   Reply With Quote
Old Apr 7th, 2006, 4:36 AM   #13
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by hydroxide
YMMV but I find the following more Pythonic ;-)
I had forgotten about slices - very neat! This is exactly what I meant about using Python efficiently; hydroxide's reverse function seems a lot more efficient and easier to read than mine. If you know the language inside and out, you can program more efficiently than someone who merely knows the language well.
Arevos is offline   Reply With Quote
Old Apr 7th, 2006, 11:39 AM   #14
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thumbs up

I find hydroxide's example ...
def reverse(x):
    return x[::-1]

for elem in ([1,2,3], (1,2,3), "123"):
    print reverse(elem)
... much more readable, than this even briefer version ...
for elem in ([1,2,3], (1,2,3), "123"):
    print elem[::-1]
... the function name tells me what is going on.

I have to agree with hydroxide's statement:
Pythonicity is about elegance and clarity of structure and intent.
Pythonic code is "pretty", as brief as it can be, but not at the expense of
readability, and written using Python idioms.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 7th, 2006, 5:24 PM   #15
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Personally I loathe ruby's "end" <shrug>. I believe that emacs pymode uses #end for enforcing indentation, if you really want it.
hehe

I also prefer "end", as long as it's not "end for" and "end if" like in some languages.

All in all, I actually far prefer Ruby, but that's another topic entirely :p
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 7th, 2006, 6:23 PM   #16
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Arg! Edit button disappears!

I misread the post. Contrary to you opinion, I prefer Ruby's "end" to Python's forced indentation
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC