[Grok-dev] Simple adapter question

Jan Ulrich Hasecke juhasecke at googlemail.com
Thu Mar 5 14:41:10 EST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Am 05.03.2009 um 20:26 schrieb Martin Aspeli:
>>> import grok
>>> from zope import interface
>>>
>>> class Fireplace(grok.Model):
>>>    pass
>>>
>>> class Cave(grok.Model):
>>>    pass
>>>
>>> class IHome(interface.Interface):
>>>    pass
>>>
>>> class Home(grok.MultiAdapter):
>>>    grok.adapts(Cave, Fireplace)
>>>    grok.implements(IHome)
>>>
>>>    def __init__(self, cave, fireplace):
>>>        self.cave = cave
>>>        self.fireplace = fireplace
>>>
>>> home = IHome(cave, fireplace)
>
> This looks fishy to me. Unless Grok is doing something very evil, this
> won't work: it'll try to adapt `cave` to IHome, and if that fails,  
> it'll
> fall back to the second argument 'fireplace'.
>
> I think what you want is:
>
>  home = getMultiAdapter((cave, fireplace), IHome)
>
>>> Why is there an IHome in the last line? I would think that
>>>
>>> home = Home(cave, fireplace)
>>>
>>> would create a home-instance.
>
> It does, and in this case, the two are probably equivalent. "Calling"
> the interface (for a single-adapter!) or using
> getAdapter/getMultiAdapter, means "Get the adapter from what I give  
> you,
> to the interface I ask for". If there was a more specific adapter to
> IHome, then you'd get that one instead.
>

So why the class Home is defined at all?

> If you can, find Brandon Craig Rhodes "using grok to walk like a duck"
> talk on video and watch it. :)

I presume that I miss something obvious to everyone else, so I'll  
watch the film again. It is really great.

juh
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (Darwin)

iEYEARECAAYFAkmwKtYACgkQPUzUEFbILMRiUACbBWCQ7IweNE6MVzapZ08fmHin
PbUAn0qecFWMH6eWOgZ7tvrd7o5rRNeq
=U9vX
-----END PGP SIGNATURE-----


More information about the Grok-dev mailing list