My take on 'Python Productivity tip for Java Programmer'. Could you give me more feedback?

Thomas Jollans t at
Mon Jul 11 18:58:06 CEST 2011

On 07/11/2011 06:42 PM, Anthony Kong wrote:
> Thomas, 
> Thanks for the excellent suggestions.
> Generator is certainly an interesting subject.
> From what i understand, the advantage of generator is mainly about
> saving memory, right? (i.e. no need to create a list in memory before
> iterate thru it)

When it comes to generator expression vs. list comprehension, yes. It
also has the benefit that, if you're reading, say, from the network, you
can do things with the content elegantly as it arrives: you can process
the first bit when the last bit doesn't yet exist.

"Classic" Python-2.3 (?) generators themselves are brilliant invention
that makes it easy to create custom iterators. Java has iterators too,
but nothing akin to this:

def foo():
   # do stuff:
       yield x

> Duck typing... Although it can be easily demonstrated, I find it hard to
> explain its advantages to Java developers who are so used to Interfaces.
> (Is it about the certainty of type info... I am not sure about their
> concern actually)

They're used to interfaces, yes. Duck typing is little more than
implicit interfaces. Python does support Java-style interfaces via ABC,
but all the metaclasses and isinstance calls are needless complexity.

> Jython is not a possibility, but I will show them an example anyway. We
> can use it to write some support script, I suppose.
> (Off topic) Monkey patching - It is a term used by Ruby developer a lot.
> If it means to change a function's implementation in run-time, i think
> python can achieve the same, right? Is it equivalent to Mixin?

Python doesn't have Ruby mixins as such. It's possible to replace
functions and methods on any object, but it is almost always better (as
in easier to understand and maintain) to modify the class/module you're
using, or to subclass it. The only case when it's useful IMHO is when
there's a bug or shortcoming in an external library you use, cannot
modify, and if subclassing can't achieve the desired results.

Don't show your Java programmers this. Being used to static typing,
they'll be terrified, and rightly so.


More information about the Python-list mailing list