[Python-Dev] One-line abstractmethod function?

Steven D'Aprano steve at pearwood.info
Fri Dec 6 11:46:21 CET 2013


On Fri, Dec 06, 2013 at 07:28:57AM +0100, Gregory Salvan wrote:
> Hi,
> maybe a syntax like that can correspond:
> 
> class MyObject(metaclass=ObjectSpec):
>     ''' MyObject doc'''
>     'attr1 contains something'
>     attr1 = None
>     'attr2 contains something'
>     attr2 = str
>     'method1 do something'
>     method1 = NotImplementedMethod('self', 'arg1', kwarg1=str)

I really don't like the idea of extending the docstring syntax to 
arbitrary objects. Docstrings are just the right amount of magic to be 
useful, without being so much as to be confusing. If you extend them to 
arbitrary objects, that crosses the line. I really don't like the look 
of the above, I'd rather see:


class MyObject(metaclass=ObjectSpec):
    ''' MyObject doc

    attr1 contains something
    attr2 contains something

    '''
    attr1 = None
    attr2 = str

    @NotImplementedMethod
    def method1(self, arg1, kwarg1=str):
       '''method1 do something'''


One-liners are over-rated, especially since there isn't that much that 
you can fit in 80 or even 100 chars on a single line. Toy examples, 
like "method1 do something", sure. But real methods that do real things? 
Not so often.

Besides, this suggestion changes the semantics of code, and therefore 
would need to go through a "from __future__ import docstring_everywhere" 
phase. Currently, bare strings can be used for multiline comments, as 
the compiler discards them at compile-time:


fe()
fi()
fo()
fum()
"""At this point, we can smell the blood of any Englishmen in the 
vicinity."""
bread = grind_bones(englishman)


Another issue is that many objects (particularly built-ins) cannot take 
additional attributes. So where are you going to attach the docstrings? 
What are you going to do with singletons like None?

"Amount of spam."""
spam = None

"Amount of eggs."""
eggs = None

print(spam.__doc__)
=> 'Amount of eggs.'



> Metaclass "ObjectSpec" would extend ABCMeta to search class source code for
> comments before members assignement,

What do you expect to happen when the source code is not available?

The behaviour of code should not depend on whether it is pre-compiled or 
not.



-- 
Steven


More information about the Python-Dev mailing list