From: Christian Heimes on
> >>> i.__add__ = __add__
> >>> i+1
> 6
> >>>
>
> Was this in reference to a specific python version?

This doesn't work with new style classes and thus not in Python 3.x.
Subclass from object and you'll see the difference.

Christian

From: Ben Finney on
Brian Blais <bblais(a)bryant.edu> writes:

> On Jun 28, 2010, at 14:25 , Chris Rebert wrote:
> > __doc__ is normally defined on classes, e.g. `A`, not instances,
> > e.g. `a`. help() looks for __doc__ accordingly.
>
> so that gets back to my original question: can I change this text at
> runtime. Doesn't look like I can, because it is defined for classes
> rather than instances. Am I thinking about this correctly?

Classes are objects. You can change the '__doc__' attribute of a class
object the same as you'd change it for any other object::

A.__doc__ = "new docstring"

--
\ “All television is educational television. The question is: |
`\ what is it teaching?” —Nicholas Johnson |
_o__) |
Ben Finney
From: Steven D'Aprano on
On Tue, 29 Jun 2010 10:37:44 +1000, Ben Finney wrote:

> Brian Blais <bblais(a)bryant.edu> writes:
>
>> On Jun 28, 2010, at 14:25 , Chris Rebert wrote:
>> > __doc__ is normally defined on classes, e.g. `A`, not instances, e.g.
>> > `a`. help() looks for __doc__ accordingly.
>>
>> so that gets back to my original question: can I change this text at
>> runtime. Doesn't look like I can, because it is defined for classes
>> rather than instances. Am I thinking about this correctly?
>
> Classes are objects. You can change the '__doc__' attribute of a class
> object the same as you'd change it for any other object::
>
> A.__doc__ = "new docstring"


True, but what you can't do is:



a = A()
a.__doc__ = "new docstring"



unless you jump through hoops with __getattribute__ or descriptors.



--
Steven



From: Thomas Jollans on
On 06/29/2010 02:37 AM, Ben Finney wrote:
> Brian Blais <bblais(a)bryant.edu> writes:
>
>> On Jun 28, 2010, at 14:25 , Chris Rebert wrote:
>>> __doc__ is normally defined on classes, e.g. `A`, not instances,
>>> e.g. `a`. help() looks for __doc__ accordingly.
>>
>> so that gets back to my original question: can I change this text at
>> runtime. Doesn't look like I can, because it is defined for classes
>> rather than instances. Am I thinking about this correctly?
>
> Classes are objects. You can change the '__doc__' attribute of a class
> object the same as you'd change it for any other object::
>
> A.__doc__ = "new docstring"
>

No, you can't. Well, yeah, you can. But you can't. But you can. Ahrgh

I want Python 2.x to go away. It's so inconsistent and silly.

% python2.6
Python 2.6.5+ (release26-maint, Jun 28 2010, 19:46:36)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class OLD: pass
....
>>> class NEW(object): pass
....
>>> OLD.__doc__ = "foo"
>>> NEW.__doc__ = "bar"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: attribute '__doc__' of 'type' objects is not writable
>>>

From: Aahz on
In article <mailman.2338.1277812368.32709.python-list(a)python.org>,
Thomas Jollans <thomas(a)jollans.com> wrote:
>
>% python2.6
>Python 2.6.5+ (release26-maint, Jun 28 2010, 19:46:36)
>[GCC 4.4.4] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>>>> class OLD: pass
>...
>>>> class NEW(object): pass
>...
>>>> OLD.__doc__ = "foo"
>>>> NEW.__doc__ = "bar"
>Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
>AttributeError: attribute '__doc__' of 'type' objects is not writable
>>>>

I'd argue that's a bug -- feel free to file one. I think this might
even be fixable in 2.7.1.
--
Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it." --Dijkstra