Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 6th, 2006, 11:36 AM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile What is your Definition of Pythonic?

My main attraction to Python is readability of code. I always thought that this was rather pythonic. Any other thoughts or disagreements?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 6th, 2006, 11:55 AM   #2
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
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
Master is offline   Reply With Quote
Old Apr 6th, 2006, 1:40 PM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

I sort of like Ruby's end, but on the other hand it is not always very easy to figure out where the end begins.

At least in Python I know that the colon starts the appropriate block indentation. However, mixed tab and space indentations, easy to do, can thoroughly ruin any Python code!

I like Ruby and Python, they both have their strengths. Compared to C++ or Java, I feel like a kid in the candy store!

So what do you call pythonic in Ruby? Rubiconic?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 6th, 2006, 1:52 PM   #4
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
Rubyist
Master is offline   Reply With Quote
Old Apr 6th, 2006, 3:21 PM   #5
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
I think C++ is way more readable, but then again I program it alsmot everyday.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Apr 6th, 2006, 4:36 PM   #6
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
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:

def reverse(x):
	result = [x[len(x) - a] for a in range(1, len(x) + 1)]

	if type(x) == list:
		return result
	elif type(x) == tuple:
		return tuple(result)
	elif type(x) == str:
		return ''.join(result)

Whereas Arevos' much more elegant, and "pythonic" code looked like this:

def reverse(s):
    return { str : "".join, list : list, tuple : tuple }[type(s)](reversed(s))

I think the differences are obvious :p
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!

Last edited by Jessehk; Apr 6th, 2006 at 4:48 PM.
Jessehk is offline   Reply With Quote
Old Apr 6th, 2006, 5:56 PM   #7
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
Basic is the most readable language (to me) but it isn't at all powerful enough to do anything I want. I like python because (to me) it has a very basic-ish feel to it.
Indigno is offline   Reply With Quote
Old Apr 6th, 2006, 6:15 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I thought pythonic was a man and woman with a non-sexual relationship .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 6th, 2006, 6:21 PM   #9
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 Master
But the way things are written in it makes it seems as if there is there a beginning but no end such
I suspect this varies from person to person. When I read code, I take a lot of information from the indentation. Misaligned code is virtually unreadable to me until it's been cleaned up. The addition of an ending "end" or "}" adds no extra information for me.

With regards to the original question of this thread, I consider a program to be "pythonic" if it uses Python in an efficient manner, in terms of programming efficiency.

For instance, this is a Pythonic way of returning two variables:
def foobar():
    return "Foo", "Bar"

a, b = foobar()
And this is a very unpythonic (but very C-like) way of doing the same:
def foobar(list):
    list.append("Foo")
    list.append("Bar")

list = []
foobar(list)
a = list[0]
b = list[1]
Like Ruby, Python is a very high level language, and a pythonic way of programming will make maximum use of Python's features. Because Python is dynamic, classes and functions can be constructed and altered at run-time, and this leads to a more abstracted method of programming. Instead of creating several similar classes, you might devise a function to automate the creation. Instead of adding logging functionally manually to each function, you might create a metaclass to automatically do so.

Another thing to remember is that whilst Python is relatively small in syntax, it contains a number of tools and techniques that are worth knowing about. Looking around at some of the recipes on Python cookbook is a good way to learn about some of the more advanced tricks of the trade.

The idea that pythonic code should be readable is also a good one. As a rough rule of thumb, I've found that better coders produce neater and more readable code than bad ones. This may be because the programmer is actively thinking about how to create readable code, or it may be because a good programmer will be thinking about the structure and architecture a lot more than a bad one, and this may reflect in the code itself.
Arevos is offline   Reply With Quote
Old Apr 7th, 2006, 12:56 AM   #10
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Arevos hits the nail on the head!
Quote:
I consider a program to be "pythonic" if it uses Python in an efficient manner!
Quote:
Originally Posted by DaWei
I thought pythonic was a man and woman with a non-sexual relationship .
You must be thinking of pathetic?
__________________
I looked it up on the Intergnats!
Dietrich 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:36 AM.

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