[Python-Dev] Can we improve support for abstract base classes with desciptors

Nick Coghlan ncoghlan at gmail.com
Thu Jun 9 04:01:01 CEST 2011


On Thu, Jun 9, 2011 at 8:51 AM, Darren Dale <dsdale24 at gmail.com> wrote:
> That should be "get_abstract_names(namespace)", since ns.items() gets
> called again in the for loop. I think the get_abstract_names function
> isn't needed though, since it is only ever called that one time. Any
> reason not replace the above block with::
>
>        abstract_names = []
>        for item in namespace.items():
>            abstract_names.extend(get_abstract_names_for_item(item))

Nope, inlining that part makes sense.

>>       for base in bases:
>>           for name in getattr(base, "__abstractmethods__", ()):
>>               # CHANGE 4: Using rpartition better tolerates weird
>> naming in the metaclass
>>               # (weird naming in descriptors will still blow up in
>> the earlier search for abstract names)
>
> Could you provide an example of weird naming?

>>> class C(object):
...   pass
...
>>> setattr(C, 'weird.name', staticmethod(int))
>>> c = C()
>>> c.weird.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'C' object has no attribute 'weird'
>>> c.weird.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'C' object has no attribute 'weird'
>>> getattr(c, 'weird.name')()
0

This is definitely something that could legitimately be dismissed as
"well, don't do that then" (particularly since similarly weird names
on the descriptors will still break). However, I also prefer the way
partition based code reads over split-based code, so I still like the
modified version.

Full tolerance for weird naming would require storing 2-tuples in
__abstractmethods__ which would cause a whole new set of problems and
isn't worth the hassle.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list