Suggesting for overloading the assign operator
Bengt Richter
bokr at oz.net
Wed Jul 2 16:09:47 EDT 2003
On Wed, 2 Jul 2003 08:21:57 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:
>Quoth Bengt Richter:
> [...suggests a := b equivalent to a.__update__(b)...]
>> I guess it's a matter of what kind of sugar you like ;-)
>
>Quite.
>
> [...syntactic support for Currency objects...]
>> I'm not sure what that would mean.
>
>I meant a language like Python but with a Currency literal syntax.
>With syntax such as, say,
> 123.45$ equivalent to Currency(123.45)
>(except, perhaps, for decimal vs binary exactness issues), the
>users could type
> a = 7$
> # ...
> a = 6$
>which is less onerous than writing Currency(7), Currency(6).
>
>This idea is well-suited to the scenario in which the users make
>pervasive use of objects of this type, and the main problem is
>having to type "Currency" over and over again.
Hm, it just occurred to me that one could have yet another form of sugar
[ It's now later, and I think this may be more than sugar ;-) ]
to help with this, by treating the dot operator slightly more symmetrically,
like, e.g., '+'.
Example, then explanation:
a = 7 .USD # (using symbols from http://www.xe.com/iso4217.htm by convention, no hard defs)
# ...
a = 6 .USD
This example uses the fact that int has no methods with plain names, so 6 .__getattribute__('USD')
will fail. Normally that would be the AttributeError end of it, but if '.' were like '+', we could
look for the r-version of __getattribute__ on the other object, analogous to __radd__. Thus
6 .USD # => USD.__rgetattribute__(6)
(note necessary disambiguating space here is not necessary in unambiguous contexts)
could return a specific currency object, e.g., if USD were defined something like e.g.,
(untested sketch !!)
--
class XXX_Currency(FixedPoint):
def __init__(self, symbol, *valprec):
FixedPoint.__init__(self, *valprec)
self.symbol = symbol
def __rgetattribute__(self, other):
return self.__class__(self.symbol, other, self.get_precision())
USD = XXX_Currency('USD')
--
Alternatively, perhaps USD could be a class from a factory instead of an instance, and have
__rgetattribute__ as a class method.
BTW,
num = 6
num = num.USD
would do the expected. I.e., this would be a dynamic mechanism, not a tweaked literal syntax.
Obviously this could be used for other quantity units than currencies, e.g.,
distance_to_moon = 384467 .km # avg dist
I.e., the mechanism is conceived in terms of an __rgetattribute__ method analogous to __radd__,
where you look for a compatible method in the other if the left arg can't handle it.
This could then allow a general mechanism for all types, not just numerics, yet constrain it
to special methods, to avoid accidental connections. Thus for an int,
6 . USD # => USD.__rgetattr__(6)
but also, this kind of binary-op attribute computation could supply missing methods, e.g.,
for simple file objects. Thus
f.readlines()
would effectively become
readlines.__rgetattribute__(f)()
if there were no readlines method on f, and this could
wrap f on the fly to provide the missing readlines method.
Am I being a troublemaker? ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list