inner classes in python as inner classes in Java

Alex Martelli aleaxit at yahoo.com
Wed Oct 15 17:52:32 EDT 2003


Carlo v. Dango wrote:

>>  Do you TRULY want to delegate all
>> attribute settings, performed on an instance of the innerclass, to the
>> corresponding 'outer' -- _except_ self.outer?
> 
> No, what a strange idea ;-) All attributes not found in the class should
> be forwarded to it's outer instance (as I want to simulate that the inner
> class instance is within the scope of its outer class instance.

What does "found" have to do with attribute setting?  In Python:

class X: pass

x=X()
x.foo=23

sets attribute named 'foo' to the value of 23 on x.  "found"...?!  What
can you possibly mean?

You'll have to decide what semantics you want to obtain when an
attribute is set on an instance of the "inner class" -- I sure can't do
it on your behalf.


>> tabs (your post was indented with tabs thus unreadable with such
>> newsreaders as KDE's KNode, Outlook Express, et al: please do
>> NOT post code indented with tabs, thanks).
> 
> why is that? has TAB-technology not reached linux yet? :) I try to
> remember it, I just prefer tabs over spaces..

There's a reason the official Python style guidelines have been
amended to prefer spaces instead of tabs, you know.  Sure, there
will be some programs who display tabs in a way compatible with
your preferences, but others won't; be conservative in what you
generate -- spaces are ensured to be displayed as you intend, tabs
are NOT, so don't use the latter in any code you send to others.


>> class B, but, in any case, this is how to do this kind of
>> automatic delegation in Python.
> 
> actually its forwarding or consultation, as self is re-bound when you
> invoke the outer..

In your Python code (and mine), it is false that "self is re-bound"
(only an instruction such as "self = ..." would do that, and there is
no such instruction in that code).  Sure, when a method A of
object B calls a method X of object Y, the local name 'self' is
bound to Y in the execution of X while the same name is bound
to B in the execution of A.  But that is not re-binding -- just the
distinction between lexically-same names in different scopes.

Anyway, the term "delegation" is common for the operation in
question - it is for example used in the "gang of 4"'s Design
Pattern book.


Alex





More information about the Python-list mailing list