RE: Wow: list of immediate subclasses?

Discussion in 'Python' started by Tim Peters, Aug 4, 2003.

  1. Tim Peters

    Tim Peters Guest

    [Roman Suzi]
    > I wonder why to have __subclasses__() at all... I recall somebody
    > (Tim Peters?) tolds us that classes are essentially just
    > dictionaries - everything else is an imagination by OO-brains
    > and fluffy operations like computing MRO. __subclasses__ aren't
    > needed for OO to work. What for they really are? Just to have nice way
    > to know subclasses? Why not to use some external framework for that
    > task, hooking object manipulation to some routine if necessary?
    > I can't find any reference to __subclasses__ in standard library!
    > (I was looking for __subclasses__ string)

    Looking for __subclasses__ won't answer it -- that's just Python exposing an
    internal mechanism for the morbidly curious <wink>.

    Look for uses of update_subclasses() instead, in typeobject.c. At the C
    level, Python doesn't want to do long-winded MRO searches for every
    attribute reference for every type implemented in C, so "percolates down"
    type object slot definitions from parent classes to their children at the
    time child classes are first created. Then the child class just has to look
    at its own definitions.

    This doesn't work, though, when a base class definition inherited by a child
    class changes in the base class later, or when a new definition appears by
    magic in the MRO (from the child's POV) before the base class originally
    inherited from. Since Python allows changing class characteristics at
    runtime, this happens (albeit not in most peoples' code). In such cases,
    the freshly-mutated type object's tp_subclasses slot (that's the C spelling
    of the Python-level __subclasses__) is used to figure out which derived
    classes need to get their type object slots updated too.

    In short, __subclasses__ exists to support efficient runtime class mutation
    at the C level. I view it much more as an accidental detail of the current
    implementation than as a property of the language (Python would still be
    Python if tp_subclasses/__subclasses__ didn't exist; CPython would simply be
    slower (and possibly much slower) for some kinds of code then -- at heart,
    it's to support an MRO-lookup caching/memoization gimmick).
    Tim Peters, Aug 4, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads

    Document Outline in VS.NET! WOW!, Jul 25, 2004, in forum: ASP .Net
    Jul 26, 2004
  2. Bodidily

    wow its been a while!

    Bodidily, Apr 13, 2004, in forum: HTML
    Apr 15, 2004
  3. Carl Banks

    Re: Wow: list of immediate subclasses?

    Carl Banks, Aug 1, 2003, in forum: Python
    Michael Hudson
    Aug 5, 2003
  4. Kamilche

    Wow, THAT never happens in C!

    Kamilche, Jun 28, 2004, in forum: Python
    Peter Hansen
    Jun 28, 2004
  5. dackz
    Feb 6, 2007