[Python-3000] ABC PEP isinstance issue Was: PEP 31XX: A Type Hierarchy for Numbers (and other algebraic entities)
Guido van Rossum
guido at python.org
Thu Apr 26 01:43:45 CEST 2007
This is a very good point. Perhaps we can come up with a way to make
isinstance and issubclass into something like GFs (without requiring
the whole GF machinery).
I'll think about it some more.
--Guido
On 4/25/07, Jim Jewett <jimjjewett at gmail.com> wrote:
> The current ABC proposal is to use isinstance as the test; Jeffrey
> Yaskin's numbers PEP highlighted the weakness there with a concrete
> example.
>
> If you need to an abstraction less powerful than an existing ABC,
> you're out of luck; you can't just assert that the existing class is
> already sufficient, nor can you expect everyone else to use multiple
> annotations.
>
> Short of allowing more __bases__ surgery, we need a function parallel
> to isinstance, which at least makes 3rd-party registration possible.
> I suspect Phillip will say that we really need to make the ABCs
> generic functions... but I disagree; I'm not sure that 3rd-party
> adapters should even be allowed by default, but it *should* be simple
> to create an ABC that does take them.
>
> Perhaps
>
> def isexample(obj, ABC):
> for cls in obj.__class__.__mro__:
> result = ABC.meets(obj, cls)
> if result:
> return result
> return False
>
> class Abstract...
>
> # override this with a dictionary to allow 3rd-party registration
> _good_enough = ()
>
> @classmethod
> def meets(cls, obj=None, objclass=None):
> if objclass is cls: # covers isinstance
> return obj
> if objclass in cls._good_enough: # Nothing is, by default
> return cls._good_enough.[objclass](obj)
>
> @classmethod
> def assert_sufficient(cls, objclass, adapter):
> cls[objclass]=adapter
>
>
> On 4/25/07, Jeffrey Yasskin <jyasskin at gmail.com> wrote:
>
> > If someone needs to split them later, they can use code like::
> > import numbers
> > class IntegralDomain(Ring): ...
> > numbers.Integral.__bases__ = (IntegralDomain,) + numbers.Integral.__bases__
>
> This only works with old-style classes, which are going away.
>
> >>> class Abstract1(object): pass
> >>> class Abstract2(object): pass
> >>> Abstract1.__bases__ = (Abstract2,) + Abstract1.__bases__
>
> Traceback (most recent call last):
> File "<pyshell#33>", line 1, in <module>
> Abstract1.__bases__ = (Abstract2,) + Abstract1.__bases__
> TypeError: __bases__ assignment: 'Abstract2' deallocator differs from 'object'
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list