On 9/4/25 13:28, Pierre Asselin wrote:
For reasons I won't go into I ended up defining classes inside of
a class body. That's not in the manual, but I see no reason it would
fail.

Nested classes certainly work. I've seen them used quite a bit in testing code, where you may want an object which is only interesting in the scope of the test class it appears in; less so elsewhere. In the end, it's mostly about namespacing. As far as "in the manual", everything's an executable statement, including class definitions, so anywhere you can syntactically put a statement you can put a class definition - top level, inside classes, inside functions, nested multiple levels deep, etc. Nesting means the inner symbols appear in the namespace created for the outer. How those are rendered by various tools may be an implementation decision of those tools? Have you tried introspecting to see if the namespaces actually look any different between the staticmethod case and the non-staticmethod case?

But then, I accidentally decorated the inner classes with
@staticmethod! It didn't break anything, but it had an interesting
side effect: help() and pydoc on the outer class shows the docs
of the inner class members. Normally only the inner classes are
listed, not their contents.

Here's a minimal example.
----------------------------------------------------------------------
class Outer:
     '''Outer class'''
     def outer_method(self):
         '''outer method'''

     class Inner0:
         '''Inner class'''
         def inner_0(instance):
             '''inner method, not seen by pydoc.'''

     @staticmethod
     class Inner1:
         '''Inner class passed through staticmethod()'''
         def inner_1(instance):
             '''inner method, visible to pydoc !'''
----------------------------------------------------------------------

Save and run pydoc, or import and run help(_module_.Outer).

Now that's *seriously* not-in-the-manual. I won't rely on that
behavior, plus the expanded documentation is a bit too much,
but I'm curious as how that interaction between staticmethod()
and help()/pydoc came about.


--
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to