Python complaints

skaller skaller at maxtal.com.au
Tue Nov 30 14:52:05 CET 1999


Will Ware wrote:
> 
> William J. King (wjk at wjk.mv.com) wrote:
> : 4. conversion of integer to a string ( which may exist but havn't found
> : yet)
> 
> Try this:
>         str(123)
> or:
>         hex(123)
> 
> : 2. would like something like  'increment'/'decrement' operators
> :         either in form:
> :                     "x++"  or "incr x" vice "x = x + 1 " but its ok
> 
> I find myself grumbling about having to type "x = x + 1". The really
> clean thing to do, given that integers are objects, would be to define
> increment and decrement methods, so you'd type something like "i.incr()".

	Integers are NOT objects in Python, they're values.
Same for strings and tuples. We can pretend they're objects,
and say they're 'immutable', but the truth is that they're
just plain values.

	My new Viper interpreter may support 'mutable'
integers and strings as follows: you write:

	x := 1
	x++
	x+=1

This notation actually creates a reference (pointer) to the value,
so x is a 'reference to an integer', and x++ actually means:

	x := x + 1

To see the difference consider:

	a = 1
	x := a
	x++
	print a,x

and you will get 1,2. The integer value a is bound to is still
immutable and cannot be changed. x++, on the other hand,
does not increment what x refers to, rather it replaces what
x refers to by the old value plus 1. 

References in 'rvalue' contexts convert automatically to the
designated value, but when used on the LHS the distinction
between a reference to a value, and a value is preserved.
[References can be used with any type, but they're not
quite as useful with tuples (since there is a mutable list
already) or with class instance objects (since they're
already mutable).

Comments on this idea appreciated.

-- 
John Skaller, mailto:skaller at maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
voice: 61-2-9660-0850




More information about the Python-list mailing list