ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Suggesting for overloading the assign operator
by Rim other posts by this author
Jul 1 2003 3:31AM messages near this date
Re: Identity inconsistency and unification of types and classes | Re: Suggesting for overloading the assign operator
Hi,

I have been thinking about how to overload the assign operation '='.
In many cases, I wanted to provide users of my packages a natural
interface to the extended built-in types I created for them, but the
assign operator is always forcing them to "type cast" or coerce the
result when they do a simple assign for the purpose of setting the
value of a variable. Borrowing an example from this newgroup, the
second assignment below ereases all Currency knowledge from variable
'c', when the user only wanted to update the value of c, not its type:

c=Currency(5)
c=7

I fully understand the workaround of doing c=Curreny(7) instead, but
users don't want to be bothered with that.

I read some of the prior discussions, but I did not see a suggestion
for permanent a solution. I would like to propose the following.

The idea is to separate the value assignment from the type assignment,
by introducing a new operator, let's say the ':=' operator.

'=' remains what it is, assigns type and value at the same time
':=' assigns only the value

For example:

> >> class myint(int): pass
...
> >> a = myint()
> >> a := 6
type(a)
<class '__main__.myint'> 
> >> a
6

In reality, := would just coerce the return type of the RHS of := to
the type of the variable on the LHS of the :=, preserving the value(s)
as best as it can.

If the type of the variable was still undefined, ':=' would behave
like '='. It might create confusion in future code, but does not break
old code.

I think it would be a nice feature to have. Comments?

Thanks,
- Rim
-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
Rim
Corey Coughlin
Rim
Aahz
Michele Simionato
Rim
Rim
Bengt Richter
Irmen de Jong
Ian Bicking
Bengt Richter
Steven Taschuk
Alan Kennedy
Ian Bicking
Cliff Wells
John Roth
Cliff Wells
Aahz
John Roth
Rim
Rim
Steven Taschuk
Michele Simionato
Terry Reedy

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved