Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Suggesting for overloading the assign operator

Thread Tools

Suggesting for overloading the assign operator

Corey Coughlin
Posts: n/a
You know, I couldn't even figure out what the whole ':=' assignment
did until I saw this message: Removed) (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

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.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM