inner classes in python as inner classes in Java

Alex Martelli aleaxit at yahoo.com
Sun Oct 19 04:49:51 EDT 2003


Carlo v. Dango wrote:
   ...
> that is a nice idea, however, how do I know which fields are defined in
> the outer class?? fields can be defined in every method and on runtime..

You can check if class A has [at this very moment] an attribute named 'x' by 
hasattr(A, 'x').

More likely, you will be interested in whether some INSTANCE a of class A
has that attribute [at this very moment], i.e. hasattr(a, 'x').

You can of course only check the situation "at this very moment", not if a
will at some future point during its lifetime acquire or lose such an 
attribute (that cannot, in general, be forecast).

Given that your purpose is to set (i.e., bind or rebind) an attribute 'x' 
either on a or on some other object b, I cannot see any usefulness in
making that decision based on whether a had that attribute now or had
it at some time in the past.  I have repeatedly asked you to show a use
case where dispatching attribute-setting in such ways serves a purpose
useful to the application program, but received no usable answer.  This
reinforces my current working hypothesis that whatever purpose you
think you need to serve this way might likely be best served otherwise,
but, without knowing of that purpose, I cannot advance further suggestions.

E.g., you've repeatedly mentioned some analogy to inner functions - which
CANNOT re-bind names in their outer scope! -- thus failing to provide any
usefulness whatsoever in understanding your perceived binding needs...!

If and when the purpose that's supposed to be served by such dispatching
is made clear, THEN mechanisms to achieve such dispatching may be
meaningfully discussed, compared, and contrasted; not before.


Alex





More information about the Python-list mailing list