A bind protocol (was Re: New __reference__ hook)

On Wed, Dec 5, 2012 at 11:09 AM, Sturla Molden <sturla@molden.no> wrote:
Keep in mind that descriptors already give you that for classes. There are other workarounds if you *really* have to have this functionality. You're right that globals (module body namespace) and locals (function body namespace) do not have that capability[1]. The main case I've heard for a generic "bind" protocol is for DRY. For instance, you create a new object with some name as an argument and then bind that object to that name in the current running namespace. This has been brought up before[2], with the canonical example of namedtuple (along with arguments on why it's not a big deal[3]). I'd expect such an API to look something like this: object.__bind__(name, namespace) object.__unbind__(name, namespace, replacement=None) namespace is the mapping for the locals/object (a.k.a. vars()) where the name is going to be bound. When an object is already bound to a name, __unbind__() would be called first on the current object. In that case, replacement would be the object that is replacing the currently bound one. At a high level the whole binding operation would look something like this: def bind(ns, name, obj): if name in ns: ns[name].__unbind__(name, ns, obj) obj.__bind__(name, ns) ns[name] = obj # or whatever If you wanted to get fancy, both methods could return a boolean indicating that the name should *not* be bound/unbound (respectively): def bind(ns, name, obj): if name in ns: if not ns[name].__unbind__(name, ns, obj): return if obj.__bind__(name, ns): ns[name] = obj # or whatever The bind protocol could also be used in the fallback behavior of augmented assignment operations. Ultimately, considering how often things are bound/unbound, I'd worry that it would be too expensive for any bind API to see the light of day. -eric [1] You *can* use your own module class to get it for "globals", sort of. This wouldn't quite work for the globals associated with functions defined in the module. [2] http://mail.python.org/pipermail/python-ideas/2011-March/009233.html, and others. [3] http://mail.python.org/pipermail/python-ideas/2011-March/009277.html

(from the "Re: New __reference__ hook" thread) On Wed, Dec 5, 2012 at 11:54 AM, Bruce Leban <bruce@leapyear.org> wrote:
The lazy/lambda part aside, such an operator would somewhat help with performance concerns and allow the "binder" to control when the "bindee" gets notified. -eric

On 12/5/2012 3:40 PM, Eric Snow wrote:
This makes no sense to me. The targets of bind statements are not Python objects and do not have methods. They may be 'slots' in a python objects or may be turned into Python objects (strings), but within functions, they are not. In CPython, function local names are turned into C ints or uints.
So this does not make much sense either. -- Terry Jan Reedy

(from the "Re: New __reference__ hook" thread) On Wed, Dec 5, 2012 at 11:54 AM, Bruce Leban <bruce@leapyear.org> wrote:
The lazy/lambda part aside, such an operator would somewhat help with performance concerns and allow the "binder" to control when the "bindee" gets notified. -eric

On 12/5/2012 3:40 PM, Eric Snow wrote:
This makes no sense to me. The targets of bind statements are not Python objects and do not have methods. They may be 'slots' in a python objects or may be turned into Python objects (strings), but within functions, they are not. In CPython, function local names are turned into C ints or uints.
So this does not make much sense either. -- Terry Jan Reedy
participants (2)
-
Eric Snow
-
Terry Reedy