proto-PEPs: __bind__ and __return__?

James T. Dennis jadestar at idiom.com
Mon May 13 02:33:11 EDT 2002


 I think this is only to open a can of flameworms but I just
 have to compose the idea and let the forum shred it.

 The = operator in Python bothers me.  We have so little 
 control over it.  We can't overload it.  

 What if we created two special methods: __bind__ and __return__
 which are (if present) invoked by the assignment operator.  If
 these special methods are not present the assignment semantics
 remain the same as they are today.

 Consider the statement:

	a = b

 ... currently this works roughly as follows:

	if not exists a:
		instantiate a variable object
	if b is an expression: 
		evaluate it, creating a new object
	look up reference to b
	bind it to a (make a refer to it)

 We all know that.  So far this doesn't give us any means to make
 'a' into a constant, nor does it give us a way to constrain 'a' to 
 allow us to force 'a' to be in a specific (portion of the) class
 hierarchy.  

 Enter the __bind__ method.  Obviously if 'a' doesn't exist then
 the new/generic object won't have an __bind__ method and everything
 will proceed as we'd expect.

 However, if hasattr('__bind__', a) then that method is called with
 a reference to self, and a reference to the target object to which the 
 binding would/might occur.  This target might be a newly instantiated
 and otherwise anonymous object: a = 1 # 1 is newly instantiated and 
 was unbound prior to this statement.  That that point, the __bind__
 method could simple say: self = target (effectively do the same as
 if we hadn't over-ridden the binding operation) or it could raise
 an exception (ValueError if the target is of the wrong time is 
 fails an issubclass() test; or possbly a new ConstantException if
 we're using __bind__ to create a constant mix-in class.
 
 >>> class Constant(object):
 ...	def __bind__(self,target):
 ...		if unbound('self'): 
 ...			self=target
 ...		else: 
 ...			raise ConstantError, 'Cannot rebind to constant'
 >>> a = Constant(5)
 >>> a = 6
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 ConstantError: Cannot rebind to constant
 
 To create a constrained class we might use something like:

 >>> class ConstrainType(object):
 ... 	def __bind__(self,target):
 ...		if unbound('self') or isinstance(target,self.__class__):
 ...			self=target
 ...		else:
 ...			raise ValueError, 'Type Constraint Violated'
 
 >>> a = ConstrainType(String("foo")) 
 >>> a = 'bar'
 >>> a
 'bar'
 >>> a = 5
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 ValueError: Type Constraint Violated
 >>>

 Once could also imagine bounded numbers (that would test that they
 were being bound with a specific range, or only to even, odds, or
 whatever).
 
 I hope that makes sense.  (BTW: I hypothesized an "unbound()" 
 function whose intent should be obvious --- I suppose it could
 be implemented with enough knowlege of the scoping rules by 
 searching globals() and locals() and ???
 



More information about the Python-list mailing list