[Python-ideas] [off topic] Allow function to return multiple values

Case Van Horsen casevh at gmail.com
Wed Jul 5 15:48:24 EDT 2017


On Wed, Jul 5, 2017 at 11:34 AM, Chris Barker <chris.barker at noaa.gov> wrote:
> On Sun, Jun 25, 2017 at 6:20 PM, Mikhail V <mikhailwas at gmail.com> wrote:
>>
>> And it reminded me times starting with Python and  wondering
>> why I can't simply write something like:
>>
>> def move(x,y):
>>     x = x + 10
>>     y = y + 20
>> move(x,y)
>>
>> Instead of this:
>>
>> def move(x,y):
>>     x1 = x + 10
>>     y1 = y + 20
>>     return x1,y1
>> x,y = move(x,y)
>
>
> you CAN do that, if x and y are mutable types.
>
> I've found that when folk want this behavior (often called "pass by
> reference" or something), what they really want in a mutable number. And you
> can make one of those if you like -- here's a minimal one that can be used
> as a counter:

[veering off-topic]

I've implemented mutable integers as part of the gmpy2 library. The
eXperimental MPZ (xmpz) type breaks many of the normal rules.

Mutable

>>> a=gmpy2.xmpz(1)
>>> b=a
>>> a+=1
>>> a
xmpz(2)
>>> b
xmpz(2)

Direct access to individual bits

>>> a=gmpy2.xmpz(123)
>>> a[0]
1
>>> a[1]
1
>>> a[0]=0
>>> a
xmpz(122)

Iterating over bits

>>> a=gmpy2.xmpz(104)
>>> bin(a)
'0b1101000'
>>> list(a.iter_bits())
[False, False, False, True, False, True, True]
>>> list(a.iter_clear())
[0, 1, 2, 4]
>>> list(a.iter_set())
[3, 5, 6]

I'm not sure how useful it really is but it was fun to write. :)

casevh


More information about the Python-ideas mailing list