Implicit or Explicit getter and setter in behavior?

Stupid question... What are the implications of:

registering a behavior without a factory, as documented here: - which causes the attribute to be stored directly on the object


the structure provided by mr.bob seen here and here - which includes a marker interface and registration of the factory and marker, and therefore requires an explicit property getter and setter?

1 Like

My approach is using this context_property() factory in behaviors:

def context_property(name, default=None):
    def getter(self, default=default):
        return getattr(self.context, name, default)

    def setter(self, value):
        setattr(self.context, name, value)

    def deleter(self):
        delattr(self.context, name)

    return property(getter, setter, deleter)



class Guideline(object):
    """ Adapter for Guideline """

    def __init__(self, context):
        self.context = context

    notice = context_property("notice")
    portlet_text = context_property("portlet_text", "")
    portlet_title = context_property("portlet_title", "")
    gl_language = context_property("gl_language")
    gl_state = context_property("gl_state", "")
    gl_date = context_property("gl_date", "")


Disclaimer : the handling of default value is possible wrong and not working in reality.

Looks like others have wondered about this situation before:

answered by @jensens - Cheers!

At the end the simple rule:

If you use schema only, the context provides the schema interface (because it stores the values as attributes)
If you use behavior-adapters, always provide a marker. Its semantically correct. If you use the adapters interface as a marker it might work, but its semantically wrong.

and a rather good explanation exists in the docs...

1 Like