Inner classes

Paul Prescod paulp at ActiveState.com
Wed Jun 13 19:08:02 EDT 2001


Mats Wichmann wrote:
> 
> Java provides inner classes.  They have some interesting properties,
> but mostly smell to me like a hack to get around things being too
> strongly typed in Java.  Probably I'm missing some great point here.

Actually, Java inner classes are mostly a hack to work around the fact
that Java doesn't have first-class functions. So in Python I can do
something like this:

but = Button()
def _onclick():
    ...
but.onclick = _onclick

In Java all of the code would have to be in a class and _onclick would
have to be defined in *another* class (because you can't pass references
to functions/methods). Thus you need inner classes.

In some languages without first-class classes, you hack classes through
function "closures". In languages without first-class functions, you
hack functions through "one-method classes". Python has both and you
don't have to hack!
-- 
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.ActiveState.com/pythoncookbook




More information about the Python-list mailing list