EntropySink

Technical & Scientific => Programming => Topic started by: -KEN- on April 11, 2008, 12:26:33 AM

Title: Python
Post by: -KEN- on April 11, 2008, 12:26:33 AM
Anyone ever use it? I'm just starting to learn it, and so far, I really like it. Right now the language sort've reminds me of a more OOP Perl (which I have to admit, I love). Perl was cool because syntactically, it's similar enough to weakly-typed C where I was more-or-less comfortable writing whatever and checking the syntax and functions as I went, and it let me focus more on getting stuff done than the nitty-gritty memory and variable details (you can decide whether that's a good or bad thing).

Python's a bit different, but still seems like it could feel like home one day. There seems to be a decent amount of hype around it lately (at least in the journalism industry, if you've ever heard of Django), although I know it's not exactly a new language.

My only gripe so far is that, instead of curly brackets indicating scope, a la "myfunction() { /*stuff*/ }", it uses indenting, meaning I'd have to write that short function/if statement/loop as

def myfunction():
    #stuff
    #more stuff
    if blah:
        #more stuff

Which I guess is nice for readability, but did make me aware of my VIM settings using large tabstops instead of individual spaces (which I've now fixed). And plus, I think there's something to be said for an insane single line of code.
 
In any case, your guys' thoughts?
Title: Re: Python
Post by: -KEN- on April 11, 2008, 12:32:05 AM
Oh, and the __blah__() syntax of certain special functions kills me. Especially when you're extending to __blah____subblah() at points -- I can't remember where I saw that, somewhere in Django, and it made me very sad. I think it's because you can't do __blah__.subblah() in whatever case that is, but don't quote me on that, because I'm probably very wrong.
Title: Re: Python
Post by: -KEN- on April 11, 2008, 01:35:47 AM
Holy shit this is hilarious
Title: Re: Python
Post by: charlie on April 11, 2008, 11:11:09 AM
I've looked at python a little bit and I'm intrigued. I've also heard lots of good things about it. Unfortunately I just don't have time or reason to do anything with it.

Maybe some day.
Title: Re: Python
Post by: micah on April 11, 2008, 11:50:22 AM
(http://imgs.xkcd.com/comics/python.png)
Title: Re: Python
Post by: Perspective on April 11, 2008, 12:28:02 PM
I love python, it's without a doubt the most programmer friendly language out there.

it doesn't scale well to large numbers of objects though in memory (like, GBs), watch out!
Title: Re: Python
Post by: Perspective on April 11, 2008, 12:29:38 PM
>My only gripe so far is that, instead of curly brackets indicating scope

That can be a pain if your code gets messed up and you need to re-indent. On the other hand everyones code looks the same, you don't have to deal with these types of style issues.
Title: Re: Python
Post by: Mike on April 11, 2008, 01:37:26 PM
Sorry but languages need to be whitespace insensitive.  For example when I write classes that have a get accesser member function it is generally done on one line:  int foo() { return bar; }
It is short and sweet.

While I'm not adverse to learning it I think there are other languages that would serve me better.
Title: Re: Python
Post by: hans on April 11, 2008, 01:44:59 PM
I looked a bit at python a while ago when I was messing with Blender but I didn't get too excited about it. It just did things a little bit weird for me.
I know it's big inside Google, but I've found more interest in Groovy lately for me quick and dirty programming.
There's just too many languages out there these days.
Title: Re: Python
Post by: Perspective on April 11, 2008, 02:45:30 PM
I once had a prof who called python "Perl for humans".
Title: Re: Python
Post by: -KEN- on April 11, 2008, 08:45:30 PM
I just love that this is all it takes to parse an XML file:
Code: [Select]
#!/usr/bin/python
#Parse the Twitter friends RSS feed to get the pertinent data
import sys
from xml.dom import minidom
doc = minidom.parse(sys.argv[1])

#Get all doc elements matiching a given tag
names = doc.getElementsByTagName("name") #Get all <status> tags
text = doc.getElementsByTagName("text") #get all <text> tags

x = 0
while x < names.length:
    print names[x].childNodes[0].nodeValue, ":", text[x].childNodes[0].nodeValue
    x+=1

Although, after writing my crime map's XML parsing routines in javascript, they seem fairly similar:

Code: [Select]
//snip...
    var type = xml.getElementsByTagName("type");
    var text;
   
    for (var i = 0; i < addrs.length; i++) {
        if(type[i].firstChild.nodeValue == "Incident") {
text = "<b>" + type[i].firstChild.nodeValue + //...
Title: Re: Python
Post by: Perspective on April 12, 2008, 12:18:25 PM
instead of "while x < names.length:" you can use "for name in names:"... one those python niceisms.
Title: Re: Python
Post by: VBprogrammer on April 12, 2008, 12:35:02 PM
Sorry but languages need to be whitespace insensitive.  For example when I write classes that have a get accesser member function it is generally done on one line:  int foo() { return bar; }

Your missing the point. In Python you will pretty much never have to write a line of boiler plate code like that.

I would actually say one of my favorite parts of Python is the lack of parenthesis matching!

And Yeah -KEN-, that was a horrible example of Python coding!   
Title: Re: Python
Post by: Mike on April 12, 2008, 11:30:13 PM
You are missing the point:  I WANT to be able to do that if I so choose.  The language is my bitch not the other way around.
Title: Re: Python
Post by: Mike on April 12, 2008, 11:31:31 PM
Also from looking at some code examples it appears as though a blank line ends a function.  This isn't cool as I tend to put blank lines in my functions to help logically group statements.
Title: Re: Python
Post by: -KEN- on April 13, 2008, 01:28:56 AM
instead of "while x < names.length:" you can use "for name in names:"... one those python niceisms.

That was how my code originally was, but then to get the same output, I would have to use:
Code: [Select]
x = 0
for name in names:
    print name.childNodes[0].nodeValue, ":", text[x].childNodes[0].nodeValue
    x+=1

And not only is that ugly, but it creates a bunch of extra objects to boot. Although I'm not 100% on how Python does everything behind the scenes, so maybe not.

But how is that better than what I've done?
Title: Re: Python
Post by: Perspective on April 13, 2008, 09:48:26 AM
Also from looking at some code examples it appears as though a blank line ends a function.  This isn't cool as I tend to put blank lines in my functions to help logically group statements.

nope. just the indentation. The function ends when the next line starts with no indent. (and with "def funtion()"):
Title: Re: Python
Post by: Mike on April 13, 2008, 11:36:41 AM
So say you had

Code: [Select]
def foo()
statement 1
statement 2
statement 3
statement 4
statement 5

statement 6
statement 7
statement 8
statement 9
Would the function end after #9 or #5?
Title: Re: Python
Post by: Perspective on April 13, 2008, 11:54:48 AM
after 9
Title: Re: Python
Post by: Perspective on April 13, 2008, 11:56:22 AM
>But how is that better than what I've done?

it's not better, its just one of those syntactic conveniences. I'm pretty sure python will just bind name to a pointer and iterate it, i don't think it will actually create an object.
Title: Re: Python
Post by: -KEN- on April 13, 2008, 12:26:54 PM
Yeah, it's not completely whitespace-sensitive, just indenting.

But in any case, for my own personal reference, could someone please explain why that was crappy Python coding? If there's a more python-y way to do it, I wanna learn!
Title: Re: Python
Post by: Mike on April 13, 2008, 01:37:15 PM
Ok thanks, so it isn't as bad as it seemed.

Can't say I'm thrilled with the indentation scoping thing though.  Over at SMF we've been fighting someone's editor magically inserting spaces at the beginning of lines, between tabs, etc.  In PHP that isn't a problem but in Python that would just wreck havoc.
Title: Re: Python
Post by: -KEN- on April 13, 2008, 07:10:17 PM
Right, I can't imagine sharing some python source across programmers with different environments and OSes.
Title: Re: Python
Post by: micah on April 22, 2008, 09:44:20 AM
another from xkcd.... the payoff is at the end

(http://imgs.xkcd.com/comics/new_pet.png)
Title: Re: Python
Post by: VBprogrammer on April 24, 2008, 08:50:23 PM
I'm starting to wonder if the XKCD guy is taking the piss re Python :)