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

Re: I think I am Python Challenged Posted: Jun 10, 2005 10:53 AM
Reply to this message Reply
Advertisement
> * closures and anonymous functions
>
def make_adder(n):
> def add(x):
> return x+n
> return add


In Java, a little more formality lets you approximate closures with the addition of some declaration.

public interface adder<T> {
    public T add( T x);
}
 
public adder make_adder( final int n ) {
    return new adder<int>() {
        public int add( int x ) {
            return x+n; 
        }
    };
}


Now, many people have argued about the whole interface and anonymous class business, but, this is exactly a closure from my perspective, and it does provide some opportunity to robustly evolve such a closure because type checking is in place.

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


The same mechanism as shown above works in this case. In Java, we are always compelled to use interfaces/types and I find this to let me meet the API contracts much more readily.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 10, 2005 11:18 AM
Reply to this message Reply
> > So, please, enlighten me.
>
> I think this is impossible for systemic reasons: you have
> to enlighten yourself :)

I'm listening/reading, but not sure I'm getting what I need yet.

> What is expressable in Python, that is not in Java? Try to
> find an interactive console where you can run Java code
> snippets.

Very seldom do I need to do this. Practically, I don't get stuck on things that require small snippets of code to try out. I have used very expressive languages at times, which have consoles, and it is there odd, variable syntax and expressivity that made it necessary to use a console to make it easier to try and find the right expression! The limited expressivity in Java feels empowering to me, less to remember.

The fact that there is not a widely used console for Java, to me, just says that its not needed by the masses. There are some interactive Java environments that do use Java's dynamic classloading and introspective capabilities to let you have a command line. However, the more strict object structure of Java makes it difficult to have an enclosing object that the lines of code you type can be encapsulated within. However, it is technically possible to make something that works, and I've used at least one Java console that did work. It was just not something that I needed to use on a regular basis.

> 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.

I use Java collection objects a lot too. I tend to use maps in many cases that others might use lists initially, because I've found that many times I need a map, later in the evolution of the application, and I can thus more easily alter the map use then swithing from list to map. Also, fast access in a list requires the extra index item. In a map, you can use the object, or some existing item in the object that the map stores.

> 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.

I can understand the convenience of using the object this way. Technically, Java's use of
public static void main( String args[] );

as the main entry point is only an implementation detail of the java interpreter that Sun and others provide with their JVM. There is nothing that keeps one from creating a different launcher to provide similar capabilities to what you describe above. The JNI mechanisms let you do this, and I've used JNI to launch a Java application as a Windows Service using a different launch interface than the typical one.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 10, 2005 11:26 AM
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.

This is not possible to do in a compile time, type checked language. The delayed binding of the name to the object is something that dynamic languages do better.

In this example of using environment variables, I'm not sure of the real value here. In Java, it is possible to create a map that has a less verbose syntax than
System.getProperty("TEMP");

That map could also provide the case insensitivity that you show in your example.

While I can see that __getattr__ might have some interesting capabilities, it really opens pandora's box for undefined references which can create some really odd bugs, especially when you use it for something that has an infinite set of attribute choices, such as os property values. This seems pretty risky to me for such a limited simplification of declaration based programming.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 10, 2005 11:36 AM
Reply to this message Reply
> > * generators, ie. the yield keyword. Like in Icon.
>
> I am not familar with generators.

Okay, the Python documentation example of this is

def generate_ints(N):
for i in range(N):
yield i
[/[pre]
This looks to me like a closure with local variables.

public interface generate_ints {
public int nextVal();
}

public void generate_ints(final int n ) {
return new generate_ints() {
int i = 0;
public int nextVal() {
if( i > n )
throw new EndOfGeneratorException(i,n);
return i++;
}
};
}

Now, it appears that yield does something funny with the exception stack and thus can't be used in a finally. That's a pretty severe restriction that will certainly not make this the solution to use everywhere.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 10, 2005 11:50 AM
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.

So, you're asserting that people become comfortable with a language because its expresivity is more in line with their way of problem solving/thinking?

I think this is the big issue myself. I'm wondering if that, in and of itself, is a compelling reason to keep creating completely new languages though...

I'm not suggesting that if you don't think a particular way, that you need to change. I'm just wondering if what we're missing is a language that has the right usability factor mixed in with its expresivity. My mind has no problem groking the Java syntax. I'm a big fan of words over punctuation chars... So, I find languages with odd syntax or excessive concise expressions to be a burden to my use.

I'm sure that we all have problems getting through certain roadblocks in using languages because I brain won't quite think the right way...

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: I think I am Python Challenged Posted: Jun 13, 2005 3:37 AM
Reply to this message Reply
> So, you're asserting that people become comfortable with a
> language because its expresivity is more in line with
> their way of problem solving/thinking?
>
> I think this is the big issue myself. I'm wondering if
> that, in and of itself, is a compelling reason to keep
> creating completely new languages though...

We definitely need to see new languages being developed, if only in the hope that someone might stumble onto something that really is different.

Unfortunately, what we are seeing, more often than not, are new variants, new syntaxes (for doing the same thing differently) being developed. I learnt to code using Pascal and Fortran and my first commercial language was COBOL. I've yet to be convinced that any application language is significantly more productive than they are. That's probably overstating things but encapsulation is the only significant new feature that modern languages offer that is internal to the language. Pretty much all other new language "features" are hooks for connecting to hardware (e.g. clients and servers, the Internet, etc.) "Language X is an order of magnitude better than language Y". I don't think so.

Underlying almost all of them is the sad fact that there has been little progress in languages in the last twenty years. We still have to tell the computer how to add x to y to get z. We still have to think about whether or not x or y are compatable. We still have to do too much.

My customers typically say to me stuff along the lines of "Can you knock up an app with produces a graph that shows the profit we're deriving from our revenue under these circumstances?". Twenty years ago we were promised we could put such a question directly to the computer (and that I'd be out of a job). Twenty years later, there's no sign that computing languages are any closer to that goal.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: I think I am Python Challenged Posted: Jun 13, 2005 3:42 AM
Reply to this message Reply
I hate to be cynical (really), but if the main difference between two languages amounts to little more than the layout of the sqwiggly brackets then my take is that both languages are, to all intents and purposes, identical.

V.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 13, 2005 7:04 AM
Reply to this message Reply
> My customers typically say to me stuff along the lines of
> "Can you knock up an app with produces a graph that shows
> the profit we're deriving from our revenue under these
> circumstances?". Twenty years ago we were promised we
> could put such a question directly to the computer (and
> that I'd be out of a job). Twenty years later, there's no
> sign that computing languages are any closer to that goal.

My recollection was that back in the early 1980's, there were TRS-80 programs and later MSDOS programs that did do some natural language problem solving for business applications related to accounting. The single platform nature of these applications, and perhaps the very focused, and limited nature of them may have caused untimely deaths. Today, there are plenty of people willing to pay money for vertical market solutions that have real business values.

I just wonder if the problems are as simple today as they were then. So many more things are done with software today. The flexibility of having remedial programming languages has allowed a dearth of customization which in turn has created many unique solutions to the same problem. While perhaps similar enough in outcome, they are very different in underlying activities.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: I think I am Python Challenged Posted: Jun 13, 2005 3:37 PM
Reply to this message Reply
Let's say Python is not quite an order of magnitude more productive than COBAL. Let's say it's only 9 times more productive. If you think that's identical, then your "intents and purposes" are a lot different than mine.

Also, isn't the stuff you mentioned before more sensibly done with a spreadsheet than a programming language?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: I think I am Python Challenged Posted: Jun 13, 2005 3:42 PM
Reply to this message Reply
I think this is the big issue myself. I'm wondering if that, in and of itself, is a compelling reason to keep creating completely new languages though...

Yeah, Java is the holy grail of languages and all other language development should stop. Reminds me of the New York (Times? Post?) editorial about how it was impossible for a rocket to work. Why even try?

I think you got the title right and further elaboration has not added any more pith.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 13, 2005 4:00 PM
Reply to this message Reply
> I think this is the big issue myself. I'm wondering if
> that, in and of itself, is a compelling reason to keep
> creating completely new languages though...

>
> Yeah, Java is the holy grail of languages and all other
> language development should stop. Reminds me of the New
> York (Times? Post?) editorial about how it was impossible
> for a rocket to work. Why even try?
>
> I think you got the title right and further elaboration
> has not added any more pith.

Matt, I sense that you are still not impressed with my comparison of python against java, and perhaps you've read my other posts stating that we ought to just stop developing other languages and just standardize on Java because it's good enough for right now. That's another topic...

In this thread I am not at all trying to go down that path. I am simply trying to discover more about why people choose python.

I am comparing the examples to Java to show how I view the issues. But, that's all I am trying to do. Show an example of why I don't see the point of myself using python.

Many people get into programming using a single language on a single project. If they don't go through computer science they may not get multi-language exposure.

If they don't take computer language design classes, they may not learn some more interesting things about language design and the complexity or commonalities of certain language features. LALR tools such as Lex/Yacc and the plethora of follow on tools has enabled a lot of language development. But, those tools also focus syntax and to some degree semantic richness down particularly common paths.

I'm trying to stir up comments and discussions specifically about where language development is going. The Java language and platform is here. It, like many languages, including python, is supported by various communities and companies.

So, I'm really curious to learn more about peoples motivation to use python. Is it because it's OS? Is it because it's cool? Is there a feature of the language that makes your application only possible on python.

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: I think I am Python Challenged Posted: Jun 14, 2005 8:15 AM
Reply to this message Reply
I think you take this far too serious. Programming is programming. In the end you still have to think. I spend most of my time writing java, there is nothing wrong with that. It is just that after I have writen code like this the 100th time (the place I am working uses java 1.4):


String[] posCodes = new String[3];
posCodes[0] = "left";
posCodes[1] = "right";
posCodes[2] = "body";
for (int i=0;i<3;i++) {
area = (LinkedHashMap)componentAreas.get(posCodes[i]);
if (area!=null) {
Iterator iter = area.keySet().iterator();
while (iter.hasNext()) {
subComponentName = (String)iter.next();
try {
sortedSubComponents.add(subComponentName);
} catch (ClassCastException e) { }
}
}
}


I wish I could be writing this instead:


posCodes=["left","right","body"]
for pos in posCodes:
area=componentAreas[pos]
if area is not None:
for subComponentName in area:
sortedSubComponents.append(subComponentName)


(disclaimer: java is some random production code snipet. Translated code is untested python code.)

No big deal. Some of us are just more sensitive to boilerplate. Nothing fundamental, just a more convenient way than java of writing stuff about every 7 lines of code.

Gregg Wonderly

Posts: 317
Nickname: greggwon
Registered: Apr, 2003

Re: I think I am Python Challenged Posted: Jun 14, 2005 9:24 AM
Reply to this message Reply
>
> String[] posCodes = new String[3];
>     posCodes[0] = "left";
>     posCodes[1] = "right";
>     posCodes[2] = "body";

String[] posCodes = { "left", "right", "body" };

accomplishes the same thing...
>     for (int i=0;i<3;i++) {
>    area = (LinkedHashMap)componentAreas.get(posCodes[i]);
>       if (area!=null) {
>         Iterator iter = area.keySet().iterator();
>         while (iter.hasNext()) {
>           subComponentName = (String)iter.next();
>           try {
>             sortedSubComponents.add(subComponentName);
>           } catch (ClassCastException e) {   }
>         }
>       }
>     }
> 

The use of some of the features in JDK1.5 lets you simplify this.

HashMap<String,LinkedHashMap<String,Object>>componentAreas =   ...
String[] posCodes = { "left", "right", "body" };
for( String code: posCodes ) {
   LinkedHashMap<String,Object>area = componentAreas.get( code );
   for( String sub: area.keySet() )
       sortedSubComponents.add(sub);
}

I'm not sure why the ClassCastException is there, but I assume it's because there is not type checking available
on the maps/lists and that's just defensive programming.

Adding the try/catch back in would add some lines back.

I know this doesn't make it quite so simple as the python example you provide, but I think there are some simplifications that make things a little less verbose.

The Generics declarations are more verbose, but eliminate
the explicit casts everywhere. And, I have to say that I did find a bug in some old software when I converted it to generics and found out that I was using the wrong map in some code...

> I wish I could be writing this instead:
>
>

> posCodes=["left","right","body"]
> for pos in posCodes:
> area=componentAreas[pos]
> if area is not None:
> for subComponentName in area:
> sortedSubComponents.append(subComponentName)
>

>
> (disclaimer: java is some random production code snipet.
> Translated code is untested python code.)
>
> No big deal. Some of us are just more sensitive to
> boilerplate. Nothing fundamental, just a more convenient
> way than java of writing stuff about every 7 lines of code.

This is a valid point, I understand the frustrations with too much verbosity. These are things that can be resolved. The JCP process is becomming a little more agile and hopefully, individuals will be able to participate more readily as Doug Lea does to make significant things happen.

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: I think I am Python Challenged Posted: Jun 14, 2005 12:33 PM
Reply to this message Reply
> >
> > String[] posCodes = new String[3];
> >     posCodes[0] = "left";
> >     posCodes[1] = "right";
> >     posCodes[2] = "body";

>
> String[] posCodes = { "left", "right", "body" };

> accomplishes the same thing...
I realized that after I posted. I just copied and pasted some code without paying much attention, there is always some optimization of the code left.
> The use of some of the features in JDK1.5 lets you
> simplify this.
>
>
> HashMap<String,LinkedHashMap<String,Object>>componentAreas
> =   ...
> String[] posCodes = { "left", "right", "body" };
> for( String code: posCodes ) {
> LinkedHashMap<String,Object>area = componentAreas.get(
> et( code );
>    for( String sub: area.keySet() )
>        sortedSubComponents.add(sub);
> }

> I'm not sure why the ClassCastException is there, but I
> assume it's because there is not type checking available
> on the maps/lists and that's just defensive programming.
>
> Adding the try/catch back in would add some lines back.
I thought it was my smart coworkers ignoring some checked exception again to make the code compile which is one of their bad habits. It seems I judged too quickly and the try block is really justified this time.
>
> I know this doesn't make it quite so simple as the python
> example you provide, but I think there are some
> simplifications that make things a little less verbose.
>
> The Generics declarations are more verbose, but eliminate
> the explicit casts everywhere. And, I have to say that I
> did find a bug in some old software when I converted it to
> generics and found out that I was using the wrong map in
> some code...

I will be happy once I can use Java 1.5. Whith each version it is getting slightly less painfull to use but also more complex as far as the feature set. Python just has been very nice to use 5 years ago and is still nice today.

Kay Schluehr

Posts: 302
Nickname: schluehk
Registered: Jan, 2005

Re: I think I am Python Challenged Posted: Jun 14, 2005 12:40 PM
Reply to this message Reply
> I wish I could be writing this instead:
>
>

> posCodes=["left","right","body"]
> for pos in posCodes:
> area=componentAreas[pos]
> if area is not None:
> for subComponentName in area:
> sortedSubComponents.append(subComponentName)
>

>
> (disclaimer: java is some random production code snipet.
> Translated code is untested python code.)
>
> No big deal. Some of us are just more sensitive to
> boilerplate. Nothing fundamental, just a more convenient
> way than java of writing stuff about every 7 lines of
> code.

Yes, but Python is not Java. Therefore you won't try to translate the Java code almost literally ( what you didn't because you skipped exception handling ) but use something like this:

for pos in ["left","right","body"]:
try:
for subComponentName in componentAreas.get(pos):
sortedSubComponents.append(subComponentName)
except TypeError:
pass


Kay

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