Suggesting for overloading the assign operator

Corey Coughlin corey.coughlin at attbi.com
Tue Jul 8 20:50:56 CEST 2003


You know, I couldn't even figure out what the whole ':=' assignment
did until I saw this message:

bokr at oz.net (Bengt Richter) wrote in message 
> I don't know that I would totally dismiss the usage. It could be
> a concise notation for a possibly polymorphic update operation.
> 
>     w = Widget(blah, blah)
>     w := ('blue','white')  # changes fg/bg colors
>     w := (20,40) # changes position
>     w := Font('Times Roman', size=24)
>     w := 'some text appended to a text widget'
> 
> Of course, you could use a property very similarly, e.g.,
>     w.p = 'text ...'
> 
> I guess it's a matter of what kind of sugar you like ;-)
> 

So what we're talking about here is some form of type driven
assignment?  Where you can assign to properties of an object given a
specific type of rhs expression?  I guess that could be useful, but it
sounds like it could only apply to a very small set of objects, namely
objects where all the properties have different types.  It seems like
adding a new syntax to support a fairly small subset of objects is a
long way to go to make things 'easier'. It looks like it would be easy
to screw up, too.  For instance:

class Point(object):
  def __init__(self, nx, ny):
    self.x = nx
    self.y = ny
  def __typeassign__(self, whatever):
    # some magic #
	
a = Point(3,4)  # so far so good
a := (3,4)	# ok, that could work
a := 3		# hmm... will it set x, or y, or x and y, or give an
exception?

or, let's go with the currency example:

class Currency(object):
  def __init__(self, cval):
    self.amount = cval
    self.exchangerate = 1.0
  def __typeassign__(self, whatever):
    #even better magic #

a = Currency(7)
a := 6         # so does this change the amount, or the exchange rate?

So it looks like the behavior here would be at best unpredictable,
unless you already have a deep understanding of what's going on in the
object.  (And if you have a deep understanding of what's going on in
the object, would 'a.amount' be so tricky to use?)  Now sure, there
are lots of operations that aren't used by every object, but assigning
values to object attributes is a pretty fundamental part of any object
oriented language, and the way Python does it now seems to work for a
lot of people.  From the original example, it looks kind of like you
want to define a sort of user-definable built in type, like integer or
string, where there is always a singular piece of data that may have
special functional attributes but no other data attributes.  But in
that case, you wouldn't want to assign it as a regular object, you'd
want to build in a special syntax that Python could use to understand
the built in type, so you would have to have:

a = $7
a = $6

so I guess even that wouldn't work quite the way you want.  So for
now, I vote no to adding ':=' assignment.  Sorry if this is too basic
a look at this, it takes me a while to wrap my head around this
complicated syntax stuff.




More information about the Python-list mailing list