[PEAK] protocols question
Phillip J. Eby
pje at telecommunity.com
Mon Mar 29 16:05:41 EST 2004
At 10:44 PM 3/29/04 +0200, Gabriel Jägenstedt wrote:
>On Mon, 29 Mar 2004 15:14:01 -0500
>"Phillip J. Eby" <pje at telecommunity.com> wrote:
> > If for some reason you would really like to have 'adapt(Food,IEdible)'
> > return a new Food instance each time it's called, then you should
> > declare an adapter instead:
> > declareAdapter(lambda ob,proto: ob(), provides=[IEdible],
> > forObjects=[Food])
> > This would cause each invocation of 'adapt(Food,IEdible)' to return a
> > new 'Food()' instance. I don't know why you would want to do that,
> > but that would be how you couldd do it if you really wanted to.
>I know I must sound quite stupid by now, but I belive it better to ask a
>stupid question once(or three times) then be stupid forever.
>By the way you put it it sounds like if I call Food with adapt more then
>once I will get the same instance the second time.
No. The opposite -- it will return a new instance each time.
>How would this work in conjunction with wanting a different
>instance of food in different classes.
> components = [Food]
> components = [Food]
>I don't want to affect the Chocolate when I eat the Strawberry.
>Unfortunatly it won't be so easy as to just adding an instance in
>Chocolate and one in Strawberry. Instead different components are called
>based on their Interface and only by method.
You probably want 'components=[Food()]', here, or to define an __init__
method that creates instances of the specified classes when an instance of
Chocolate or Strawberry is created.
Alternatively, if you only want *one* instance of each class, then you
should do something like this:
# define Food class here
Food = _Food()
# define Chocolate
components = [Food]
Chocolate = _Chocolate()
...and so on.
This is not necessarily the best way to do this, but it's the quickest for
me to explain, and since it doesn't really relate to PyProtocols I don't
want to delve into it further at the moment.
>something like: adapt(Food(), IEdible).method() each methods calls the
>next component in a generator. when the generator halts the instances
>are forever lost aren't they?
Right. So don't do that. :)
More information about the PEAK