The Artima Developer Community
Sponsored Link

Weblogs Forum
I think I am Python Challenged

46 replies on 4 pages. Most recent reply: Aug 24, 2005 8:25 PM by Gregg Wonderly

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 46 replies on 4 pages [ 1 2 3 4 | » ]
Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

I think I am Python Challenged (View in Weblogs)
Posted: Jun 5, 2005 8:59 PM
Reply to this message Reply
Summary
The Python Challenge recently caught my attention and I thought I'd look at it to see what was up. I'm not sure I caught on to the reasoning behind it, but I still played with it, for a while...
Advertisement
The Python challenge caught my attention. I don't use python, and so I haven't really learned it. I am still not warm toward the indentation based block designations, but that's another issue for another time.

What I did with the Python challenge was try and solve the puzzles without python. It turned out to be pretty easy to do the first 4 or 5 with bourne shell command line tools such as tr(1), sed(1), awk(1) and grep(1). BTW, if you are a grep(1) fan, you should check out Bill Tannenbaum's cgrep tool. It is typically 1 to 2 orders of magnitude (10 to 100 times) faster than any other grep I've ever seen. Try it on really huge files (10M or larger) and see what you think.

I've blogged before about how many of these dynamic languages aren't providing any new tools, just recasting old things in new ways. Now, I am a proponent of recasting APIs to speak the language of a problem domain so that you can easily solve the problem and so that people reading the code can follow easily.

I'm just still looking at python and asking why. If you have a favorite language feature of python, that is not available in Java, nor expressable in Java, or the .NET/CLR languages, post it here to help me see what you are appreciating. I'm aware of many of the features of python only casually from seeing them in on line discussions. So, please, enlighten me.


bug not

Posts: 41
Nickname: bugmenot
Registered: Jul, 2004

Re: I think I am Python Challenged Posted: Jun 5, 2005 11:09 PM
Reply to this message Reply
it is not what you can do, it is about how you do it. in the sense of what you do, all languages are equivalent.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: I think I am Python Challenged Posted: Jun 6, 2005 1:41 AM
Reply to this message Reply
That's funny. There's a typo if you include the L in ther first one: "The L in unnecessary."

Girts Kalnins

Posts: 23
Nickname: smejmoon
Registered: May, 2003

Re: I think I am Python Challenged Posted: Jun 6, 2005 3:41 AM
Reply to this message Reply
> I'm just still looking at python and asking why. If you
> have a favorite language feature of python, that is not
> available in Java, nor expressable in Java, or the
> .NET/CLR languages, post it here to help me see what you
> are appreciating. I'm aware of many of the features of
> python only casually from seeing them in on line
> discussions. So, please, enlighten me.

Most probably you can express language features of one language in terms of other language.

In Python you can do it by using less code than in Java. So you can spend your time on other aspects than typing and getting syntax right.

I like Python better because it's straight-forward, clean and mature. Twisted humour aspect that cames from Monty Python roots may not appleal to everyone :-)

As far as features, I guess you can not have signature based polymorphism for starters :-) And as far as I see last versions of Java are getting closer and closer to Python.

Joe Cheng

Posts: 65
Nickname: jcheng
Registered: Oct, 2002

Re: I think I am Python Challenged Posted: Jun 6, 2005 8:04 AM
Reply to this message Reply
Some of the more dynamic stuff you can do in Python is difficult/impossible with Java and mainstream CLR languages, AFAIK. Check out __getattr__ (that's two underscores on each side), decorators, and metaclasses.

If you're having trouble getting excited about Python, you might want to take a stab at Ruby or Boo, each of which I personally found more eye-opening than Python (though for very different reasons).

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: I think I am Python Challenged Posted: Jun 6, 2005 11:27 AM
Reply to this message Reply
What you did with grep, sed, awk is almost as easy in python. In java it would have required more work. But for things you could not use grep and co. and have had to switch to java you can keep using python.

Steve Donovan

Posts: 24
Nickname: stevedonov
Registered: May, 2005

Re: I think I am Python Challenged Posted: Jun 7, 2005 1:02 AM
Reply to this message Reply
> If you're having trouble getting excited about Python, you
> might want to take a stab at Ruby or Boo, each of which I
> personally found more eye-opening than Python (though for
> very different reasons).

I'd endorse that. The Boo project may end up giving us the best of both worlds; an informal, very expressive syntax, but essentially remaining statically-typed. (As a consequence, Boo programs can be much faster than 'authentic' IronPython.) Boo will deduce most declaration types, except for things like function arguments which are precisely where the poor maintenance programmer needs the most help:

def sqr(x as double):
return x*x

# parentheses here indicate an array type
def list_customers(custs as (Customer)):
for c in custs:
print c.Name

I use Boo much like I suspect people use Jython; as a fast language to prototype code using the framework libraries, which finally gets translated into an official language ;)

I mean, try selling your boss on using a language called Boo !

steve d.

Kay Schluehr

Posts: 302
Nickname: schluehk
Registered: Jan, 2005

Re: I think I am Python Challenged Posted: Jun 7, 2005 11:52 AM
Reply to this message Reply
> I'm just still looking at python and asking why. If you
> have a favorite language feature of python, that is not
> available in Java, nor expressable in Java, or the
> .NET/CLR languages, post it here to help me see what you
> are appreciating. I'm aware of many of the features of
> python only casually from seeing them in on line
> discussions. So, please, enlighten me.

I think this is impossible for systemic reasons: you have to enlighten yourself :)

What is expressable in Python, that is not in Java? Try to find an interactive console where you can run Java code snippets. I consider Python as an interactive OO language much like I consider LISP as an interactive FP language. This influences my programming style in Python because I always try to define objects, that have an intuitive appeal, that behave on a console like numbers, lists, dicts or other simple stuff. Therefore a user that deals with objects in a naive way is always somehow integrated in the program itself. Another related issue: each Python module can be the __main__ entry point of the interpreter. Each module has two sides: one as a standalone program and the other as a part of a more complex system which is again focussed in a module, which is part... This is why I indeed believe that Python is a language enabled for building large systems and I expect more of them on the market in future.

Kay

Scott Tsai

Posts: 3
Nickname: scottt
Registered: Jun, 2005

Re: I think I am Python Challenged Posted: Jun 7, 2005 3:25 PM
Reply to this message Reply
* list and dictionary data types with literal syntax
[2, 4, 3], {'bob':34.3, 'jerry':12.0}

* generator expressions / list comprehensions
sum(x*x for x in range(10))
def qsort(l):
if len(l) <= 1:
return l
return (qsort([x for x in l[1:] if x < l[0]])
+ l[0:1]
+ qsort([x for x in l[1:] if x >= l[0]]))

* generators, ie. the yield keyword. Like in Icon.
* relatively easy to use C interface for reusing C libraries
In general, Python helps me mix Functional Programming techniques with other mostly Object Oriented style to good effect.

Scott Tsai

Posts: 3
Nickname: scottt
Registered: Jun, 2005

Re: I think I am Python Challenged Posted: Jun 7, 2005 3:36 PM
Reply to this message Reply
* closures and anonymous functions
def make_adder(n):
def add(x):
return x+n
return add

signal.signal(signal.SIGALRM,
lambda n,f: sys.stderr.write('signal_handler\n'))

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: I think I am Python Challenged Posted: Jun 7, 2005 10:27 PM
Reply to this message Reply
To illustrate Joe's point, here a simple example of __getattr__():
class EnvironmentVariables(object): 
def __getattr__(self, name):
return os.environ.get(name,'')

env = EnvironmentVariables()

Now, to get an environment variable, like $(TEMP), you can use env.temp, or env.Temp, or env.TEMP. Show me some Java code that does the same thing.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: I think I am Python Challenged Posted: Jun 8, 2005 3:47 AM
Reply to this message Reply
Isn't there talk http://www.artima.com/weblogs/viewpost.jsp?thread=98196 of removing 'lamda' from Python, on the grounds that Python's owner Guido regards it as an unwanted hack?

Keith Gaughan

Posts: 17
Nickname: kgaughan
Registered: Feb, 2003

Re: I think I am Python Challenged Posted: Jun 9, 2005 5:55 AM
Reply to this message Reply
It would be more accurate to say that Guido is removing it because it's redundant.

Joe Cheng

Posts: 65
Nickname: jcheng
Registered: Oct, 2002

Re: I think I am Python Challenged Posted: Jun 9, 2005 11:39 AM
Reply to this message Reply
> It would be more accurate to say that Guido is removing it
> because it's redundant.

It is redundant if you don't mind giving all of your closures a name, and defining them before using them. Right?

And it is also a hack, a fact which is very well-documented. Although GvR has nothing to be ashamed of as apparently he didn't write it, and apparently doesn't personally use it.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 10, 2005 10:42 AM
Reply to this message Reply
> * list and dictionary data types with literal syntax
>
[2, 4, 3], {'bob':34.3, 'jerry':12.0}


With varargs support in Java, the list thing is easy to do.
With some clever programming constructs you can do

new Map<String,double>().add("bob",34.3).add("jerry",12.0)

which is a bit more verbose, but, I do like to see some explicit indication of what code is doing.

When I designed my RCL language back in 1990, I copied this kind of abreviated construction from the keykit language that Tim Thompson did for his midi language. I had lists as python does, and I used {name=val, name2=val} notation for maps. So, I am familar with the convenience of this...

> * generator expressions / list comprehensions
>
sum(x*x for x in range(10))

One can define a method called range(int) that returns <List<Integer>> and then use some of the loop abreviations in JDK1.5. But, you will have some extra variables.

What I notice the most about these clever, notational shortcuts is that they are typically used in line, in places that I would be compelled to write a simple method to do the same so that there was a name for the operation that made it more obvious what I wanted to happen.

> def qsort(l):
> if len(l) <= 1:
> return l
> return (qsort([x for x in l[1:] if x < l[0]])
> + l[0:1]
> + qsort([x for x in l[1:] if x >= l[0]]))


Collections.sort(...) takes care of sorting things, and one can define the appropriate objects/methods to make these sorted versions to be usable as R-values.

> * generators, ie. the yield keyword. Like in Icon.

I am not familar with generators.

> * relatively easy to use C interface for reusing C
> libraries

This is a crutch to me. But, I guess I do less integration at the code level where there is such needs. I do write some JNI code from time to time, and I don't find it to be difficult. In my RCL language, I had a simple mechanism for attaching native code libraries. It's hard to manage the interaction between garbage collected objects and native objects without copying...

> In general, Python helps me mix Functional Programming
> techniques with other mostly Object Oriented style to good
> effect.

I'm still not finding anything but some brevity. I have no issues with how much I have to type. I am capable of typing with both hands and so brevity of typing is not something that I consider first. I do consider the encapsulation of complex actions where recoding can create bugs as a more compelling issue...

Flat View: This topic has 46 replies on 4 pages [ 1  2  3  4 | » ]
Topic: The Final Performance Testing Example Previous Topic   Next Topic Topic: Punchy Design

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use