python - Issue with zope.component subscriber adapters adapting multiple objects -
given following code:
from zope.component import getglobalsitemanager, adapts, subscribers zope.interface import interface, implements class a(object): pass class b(object): pass class c(b): pass class ab(object): implements(interface) adapts(a, b) def __init__(self, a, b): pass class ac(object): implements(interface) adapts(a, c) def __init__(self, a, c): pass gsm = getglobalsitemanager() gsm.registersubscriptionadapter(ab) gsm.registersubscriptionadapter(ac) = a() c = c() adapter in subscribers([a, c], interface): print adapter
the output produces is:
<__main__.ab object @ 0xb242290> <__main__.ac object @ 0xb2422d0>
why instance of ab returned? ab declares adapts , b. there way can achieve behavior ac returned?
you listing subscribers. subscribers notified of things implement interface(s) interested in.
c
subclass of b
, b
subscriber interested, , notified. fact c
implements little more of no concern b
subscriber, object implement at least b
interface.
subscribers generic, want objects implement interface or subclasses thereof. adapters more specific:
>>> gsm.registeradapter(ab) >>> gsm.registeradapter(ac) >>> zope.component import getadapters >>> adapter in getadapters((a, c), interface): ... print adapter ... (u'', <__main__.ac object @ 0x104b25a90>)
getadapters()
enumerates registered adapters, names:
>>> class anotherac(object): ... implements(interface) ... adapts(a, c) ... def __init__(self, a, c): pass ... >>> gsm.registeradapter(anotherac, name=u'another') >>> adapter in getadapters((a, c), interface): ... print adapter ... (u'', <__main__.ac object @ 0x104b25ed0>) (u'another', <__main__.anotherac object @ 0x104b25a90>)
Comments
Post a Comment