B
boisgera
I had a look at the new reference implementation of PEP 246
(Object Adaptation) and I feel uneasy with one specific point
of this new version. I don't fully understand why it checks
if *the type of* the protocol has a method "__adapt__":
...
# (c) then check if protocol.__adapt__ exists & likes obj
adapt = getattr(type(protocol), '__adapt__', None)
...
As a consequence of this change, you can't define a protocol
as a class that implements __adapt__ anymore.
class Protocol(object):
def __adapt__(self, obj):
...
Instead, you may inherit from a class "AdaptingProtocol" ...
class Protocol(AdaptingProtocol):
@classmethod
def adapt(cls, obj):
...
.... that uses a specific metaclass
class MetaAdaptingProtocol(type):
def __adapt__(cls, obj):
return cls.adapt(obj)
class AdaptingProtocol:
__metaclass__ = MetaAdaptingProtocol
@classmethod
def adapt(cls, obj):
pass
That seems to be a bit complicated ... A I missing something ?
SB
(Object Adaptation) and I feel uneasy with one specific point
of this new version. I don't fully understand why it checks
if *the type of* the protocol has a method "__adapt__":
...
# (c) then check if protocol.__adapt__ exists & likes obj
adapt = getattr(type(protocol), '__adapt__', None)
...
As a consequence of this change, you can't define a protocol
as a class that implements __adapt__ anymore.
class Protocol(object):
def __adapt__(self, obj):
...
Instead, you may inherit from a class "AdaptingProtocol" ...
class Protocol(AdaptingProtocol):
@classmethod
def adapt(cls, obj):
...
.... that uses a specific metaclass
class MetaAdaptingProtocol(type):
def __adapt__(cls, obj):
return cls.adapt(obj)
class AdaptingProtocol:
__metaclass__ = MetaAdaptingProtocol
@classmethod
def adapt(cls, obj):
pass
That seems to be a bit complicated ... A I missing something ?
SB