AdaptionFailure: How to use Interfaces with PyProtocols ?
Jay Parlar
jparlar at cogeco.ca
Fri Feb 10 11:04:01 EST 2006
On Feb 10, 2006, at 4:21 AM, Nebur wrote:
> Hi,
> I tried to understand the docs of Peak's PyProtocols, and failed.
> I use PyProtocols v0.93 final. I fetched the ...tar.gz file for Linux
> and installed it using the setup.py.
> Here's my Hello-World-like example, that defines a Duck, which
> "implements" the given Interface:
>
>
> from protocols import Interface,adapt,advise
>
> class Quackable(Interface):
> def quack(loudness):
> """ print how loud to quack """
> class Duck:
> advise(instancesProvide=[Quackable,])
> def quack(self, loudness):
> print "quack! %s loud"%(loudness)
>
> if __name__ == "__main__":
> d = adapt(Duck, Quackable) # this line raises the failure
> d.quack(3)
>
You've *almost* got it. The adaption adapts *instances* of a class.
Your setup is correct, but change your __main__ to:
if __name__ == "__main__":
d = Duck()
adapted_d = adapt(d, Quackable)
adapted_d.quack(3)
or more concisely:
if __name__ == "__main__":
d = adapt(Duck(), Quackable)
d.quack(3)
Of course, it's kind of a pointless example, because you're adapting
something that declares to be Quackable to a Quackable object, but I'm
sure you know that :)
Most of my own work with PyProtocols would not involve objects that
just had 'instancesProvide', but would also have 'asAdapterFor' (at
least, I think it's 'asAdapterFor', it's been a few months since I've
touched my PyProtocols related code)
Jay P.
More information about the Python-list
mailing list