Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to set doc-string of new-style classes

0 views
Skip to first unread message

harold fellermann

unread,
Jan 11, 2005, 1:26:58 PM1/11/05
to pytho...@python.org
Hello,

does anyone know a way to set the __doc__ string of a new style class?
Any attempt I tried results in the following error:

Python 2.4 (#1, Dec 30 2004, 08:00:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object) :
... pass
...
>>> Foo.__doc__ = "bar"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: attribute '__doc__' of 'type' objects is not writable

I can understand someone arguing that "changing a doc string is during
programm execution and should therefore be forbidden!" But, I cannot
even find out a way to set the doc string, when I CREATE a class using
type(name,bases,dict) ... At least this should be possible, IMHO.

- harold -

--
If you make people think they're thinking, they'll love you;
but if you really make them think they'll hate you.

Alex Martelli

unread,
Jan 11, 2005, 1:35:08 PM1/11/05
to
harold fellermann <harold.f...@upf.edu> wrote:
...

> But, I cannot
> even find out a way to set the doc string, when I CREATE a class using
> type(name,bases,dict) ... At least this should be possible, IMHO.

>>> x=type('x',(),dict(__doc__='hi there'))
>>> x.__doc__
'hi there'

Alex

Peter Otten

unread,
Jan 11, 2005, 1:37:46 PM1/11/05
to
harold fellermann wrote:

> programm execution and should therefore be forbidden!" But, I cannot
> even find out a way to set the doc string, when I CREATE a class using
> type(name,bases,dict) ... At least this should be possible, IMHO.

That's what the dict parameter is for:

>>> T = type("T", (object,), dict(__doc__="what you want"))
>>> T.__doc__
'what you want'

Peter


harold fellermann

unread,
Jan 11, 2005, 4:33:10 PM1/11/05
to pytho...@python.org

yes, you're right ... a subsequent question, that puzzles me:
where can I apply for the "most-stupid-question"-award, now *g*

thanks anyway,

- harold -

--
The opposite of a correct statement is a false statement.
But the opposite of a profound truth may be another profound truth.
-- Niels Bohr

0 new messages