[Python-talk] October meeting topics

Kent Johnson kent37 at tds.net
Fri Sep 23 12:06:25 EDT 2005


Tom Bigford wrote:
> Python wrote:
> 
>>On Thu, 2005-09-22 at 12:46 -0400, Tom Bigford wrote:
 >>Java is missing basic functional abilities and has a relatively clumsy
>>approach to handling objects.  For instance, Python's functional (and
>>dynamic) abilities allow me to create database table classes on the fly
>>by inspecting (e.g. with MySQL command 'DESCRIBE <tablename>') tables
>>and filling in the field names and type info.
>>
>>To iterate through *ANY* container in Python:
>>	for thing in container:
>>		thing.process()		# or maybe process(thing)
>>
>>in Java (I needed to look it up)
>>	Iterator it = container.iterator()
>>	while (it.hasNext()) {
>>		Object obj = it.next()	// actually you would have some sort of cast here
>>		obj.process()
>>	}
>>In Java a hashmap is *not* a container and does not have an iterator.
>>You need to do
>>	Iterator it = hashmap.keys().iterator()
>>(That may be obvious to real Java guy, but I spent a long time wading
>>through the documentation before I found how to do that.  I was trying
>>to figure out how hashmaps lost their iterator method?)
>>
>>Initializing lists and hashmaps in Java is very clumsy.  
>>
>>The mixing of Integer objects (allocated from the heap) and int fields
>>(allocated from the stack) creates confusion and complications -
>>admittedly at some gain in performance.
>>
>>  
>>
> Thanks for the specific responses.  I believe that those "shortcomings" 
> are a matter of language expertise, rather than true problems with the 
> language

If you are saying that Lloyd just doesn't understand Java well enough, you are badly misunderstanding his points.

Java does not have first-class functions, period. Interfaces and anonymous classes can substitute in *some* cases but with vastly greater complexity. Some uses are just too painful to think about. See the "First class functions" section of this essay for examples:
http://personalpages.tds.net/~kent37/blog/stories/18.html

The clean and pervasive integration of lists and dicts into Python makes using them qualitatively different from using the Java equivalents. When you can create and initialize or iterate a list or dictionary in a single line of code you start to think about them and use them differently. Again, see the article above for examples. Nested lists and dicts are trivially easy to create in Python.

One of my favorite examples is iterating key, value pairs of a dictionary. In Python, from memory:
for key, value in d.iteritems():
  # do something with key and value

In Java (assuming key and value are Strings), with several trips to the javadocs:
for (Iterator entryIter=d.entrySet().iterator(); entryIter.hasNext();) {
  Map.Entry entry = (Map.Entry)entryIter.next();
  String key = (String)entry.getKey();
  String value = (String)entry.getValue();
  // do something with key and value
}

Is there any doubt that the first one is more readable, more writable, more usable?

> So far we are speaking of only coding, clearly an important issue  ... 
> To me, more important issues are:
> 
> 1.  ease of maintenance, including readability.  I can't speak to that 
> for python, but i know that well crafted Java is very readable.

Well written Python is at least as readable as Java, I think more readable because it is less cluttered.
> 
> 2.  scalability.  Large projects are rampant - is python suited?

Hmm, Plone anyone? Mailman? It is harder to create a large project in Python, actually - you just don't need that many lines of code to get the job done ;-)
http://www.mindview.net/WebLog/log-0036
http://mail.python.org/pipermail/python-list/2004-March/213134.html
http://pythonology.com/success
> 
> 3.  extendability.   are inheritance and polymorphism an inherant part 
> of the language?

Yes. Polymorphism is more a part of Python than Java. Google "duck typing".

> 
> I may be mistaken, but i can't remember any discussion of multi-person 
> projects in python, or projects that have to be maintained.  All of 
> these things are, of course, important to any 21st century programmer.

Python is not Perl. Python code is readable and maintainable.
> 
>>When forced to write code in Java I feel like I am in a potato sack
>>race, hindered by a set of artificial constraints.
>>  
>>
> 
> again, i believe this to be only a matter of Java experience.  i believe 
> artificial constraints to be the main feature of HL progranmming 
> languages ;-)

Sheesh. That just sounds arrogant. I had several years of Java experience before I got serious about Python and I share Lloyd's feeling. I am a professional programmer with 30 years experience. At various times in my career I have been expert at C, C++ and Java. If I had my choice I would just write Python. When I code in Java I frequently think, "This is way too hard" or "Why did they make it so hard to do this?"

Python is a very pragmatic language. Stuff you do a lot is easy to do. Writing Python code is fun like no other language I know. Python fits my brain.

Here's an interesting image. Imaging reading about sex, watching x-rated movies, listening to descriptions of sex from participants when you have never tried it. It would be easy to dismiss it - it's weird, it's messy, you get so sweaty, what's the big deal anyway? I remember as a kid my incredulity - he puts it *where*? Then you try it and you realize you had no clue what you were talking about...

Ok Python is not as much fun as sex but you really have to try it to understand that this stuff does matter, that there is a qualitative difference in the experience...

Kent



More information about the Python-talk mailing list