Suggesting for overloading the assign operator

Alan Kennedy alanmk at hotmail.com
Tue Jul 1 20:01:31 CEST 2003


Rim wrote:

> What do you think about providing a statement to assign values
> without assigning type?
> 
> '=' statement assigns value and type
> ':=' statement assigns value ony

I think the concept has some merit. I think that sometimes it useful to ensure
that the target of a rebind operation have the same type as the object which was
bound before the rebind.

I know that the same effect can be achieved by descriptors or overriding
"setattr" et al. Or something like this

a = 9
b = 42
if isinstance(b, type(a)):
    a = b
else:
    raise TypeError("Incompatible types.")

However, these options, i.e. putting type checking logic in "setattr" et al, do
not make it explicit in the code that type enforcement is in place.

Expressing the enforcement syntactically, as Rim suggests, would make it more
explicit for the programmer, as well as providing some extremely useful
optimisation hints, especially for products such as psyco. 

Psyco, if I understand it correctly, generates machine code for all paths
through a suite, depending on the types of the objects manipulated in the suite.
Such explicitly stated type enforcement would provide valuable information to
optimisers.

Not that I'm proposing that the OPs solution be adopted. Just that I think he is
addressing a valid concern.

regards,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list