class object access in class definition?

Steve Holden sholden at holdenweb.com
Thu Mar 8 04:01:26 CET 2001


"Lars Damerow" <lars at pixar.com> wrote in message
news:Pine.LNX.4.21.0103071835560.6130-100000 at zero...
> I figured that the my problem stems from when Python binds the class name
to
> the scope--when it encounters the "class" keyword, or when the class
definition
> completes. It seems like it happens when the class definition completes.
>
> What I'm trying to do is automatically register the derived classes of a
base
> class at compile time, like this:
>
> derivedClasses = []
>
> class BaseClass:
>     pass
>
> class DerivedClass(BaseClass):
>     derivedClasses.append(DerivedClass)
>
> This doesn't work because DerivedClass doesn't seem to be bound at the
time of
> the append() call. I can put the append() call in the DerivedClass's
> constructor, but that requires an instance to be created before the class
can
> be registered.
>
> If this isn't possible, it's no big deal; its goal is to avoid duplicating
> information in the code, nothing more.
>

Well, if you're prepared to be pragmatic about it, what's wrong with:

derivedClasses = []

class BaseClass:
    pass

class DerivedClass(BaseClass):
    pass

derivedClasses.append(DerivedClass)

Not elegant, but practical.

regards
 Steve






More information about the Python-list mailing list