Dynamic generation of doc-strings of dynamically generated classes

M

Mikael Olofsson

Hi!

I've asked Google, but have not found any useful information there.

Situation: I have a base class, say
ImportantClassAttribute = None

Now, I want to dynamically generate subclasses of base. That's not a
problem. However, I very much want those subclasses to have individual
doc-strings. More precicely, I want that important class attribute to be
reflected in the doc-string. That's the problem. The only way I've
managed to accomplish that is something like the following.
"""%s"""
pass ''' % (docString,))'The case 7.'

This works as intended. The subclasses do get the doc-strings I want
them to have, and I can live with this solution. But: This solution does
not strike me as especially beautiful or readable. My first naïve
attempt was instead the following.
% new.__dict__)

Traceback (most recent call last):
File "<pyshell#35>", line 1, in -toplevel-
new.__doc__ = ('The case %(ImportantClassAttribute)s.'
TypeError: attribute '__doc__' of 'type' objects is not writable

This is readable to me, but apparently not the way to go, since I'm not
allowed to replace the doc-string like this. I've also tried a number of
other ways, but they all stumble on similar reasons.

Any ideas? Am I stuck with the clumsy exec-solution, or are there other
ways to dynamically generate doc-strings of classes?

/MiO
 
A

Alex Martelli

Mikael Olofsson said:
Any ideas? Am I stuck with the clumsy exec-solution, or are there other
ways to dynamically generate doc-strings of classes?

The best way to make classes on the fly is generally to call the
metaclass with suitable parameters (just like, the best way to make
instances of any type is generally to call that type):

derived = type(base)('derived', (base,), {'__doc__': 'zipp'})


Alex
 
G

George Sakkis

Mikael Olofsson said:
Hi!

I've asked Google, but have not found any useful information there.

Situation: I have a base class, say

ImportantClassAttribute = None

Now, I want to dynamically generate subclasses of base. That's not a
problem. However, I very much want those subclasses to have individual
doc-strings. More precicely, I want that important class attribute to be
reflected in the doc-string. That's the problem. The only way I've
managed to accomplish that is something like the following.

"""%s"""
pass ''' % (docString,))
'The case 7.'

This works as intended. The subclasses do get the doc-strings I want
them to have, and I can live with this solution. But: This solution does
not strike me as especially beautiful or readable. My first naïve
attempt was instead the following.

% new.__dict__)

Traceback (most recent call last):
File "<pyshell#35>", line 1, in -toplevel-
new.__doc__ = ('The case %(ImportantClassAttribute)s.'
TypeError: attribute '__doc__' of 'type' objects is not writable

This is readable to me, but apparently not the way to go, since I'm not
allowed to replace the doc-string like this. I've also tried a number of
other ways, but they all stumble on similar reasons.

Any ideas? Am I stuck with the clumsy exec-solution, or are there other
ways to dynamically generate doc-strings of classes?

There's nothing specifically about doc-strings, but you can create and customise a whole class
dynamically:

def makeBaseSubclass(impClassAttr):
return type('new_%s' % impClassAttr,
(base,object),
{'ImportantClassAttribute': impClassAttr,
'__doc__': 'The case %s' % impClassAttr})
'The case 7'


HTH,
George
 
M

Mikael Olofsson

Alex said:
The best way to make classes on the fly is generally to call the
metaclass with suitable parameters (just like, the best way to make
instances of any type is generally to call that type):

derived = type(base)('derived', (base,), {'__doc__': 'zipp'})

and George Sakkis said something similar.

Thanks, both of you. As I expected, there was a much better way than my
clumsy way. Anyway, this took me to section 3.3.3 in the reference
manual, and that will help me further.

Thanks again. Back to the keyboard!

/MiO
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top