I’m creating a class in Python, which then has some nested classes added using setattr().
class T( object ):
def __init__( self ):
cDict = {}
cDict['__doc__'] = 'Inner class doc string'
setattr( self, 'C', type('C', (), cDict ) )
However, calling help( T ) does not then include and information about C. Constructing a T, and then a C within it, works fine.
Doing this the traditional way works fine:
class T2( object ):
class C2( object ):
__doc__ = 'Inner class doc string'
Calling help( T2 ) displays information about C2.
Can somebody shed some light on what’s happening here? Thanks.
The two snippets you showed are not equivalent.
This:
will set a
Cattribute on eachTinstance:This is because you placed the setattr inside
__init__.While this:
will add an attribute on
T2itself: