[issue1172711] long long support for array module

Meador Inge report at bugs.python.org
Tue Sep 13 04:15:40 CEST 2011


Meador Inge <meadori at gmail.com> added the comment:

>>>> import array, struct
>>>> a = array.array('L', [1,2,3])
>>>> class T(object):
> ...     def __init__(self, value):
> ...         self.value = value
> ...     def __int__(self):
> ...          return self.value
> ...
>>>> a = array.array('L', [1,2,3])
>>>> struct.pack_into('L', a, 0, 9)
>>>> a
> array('L', [9, 2, 3])
>>>> a[0] = T(100)
>>>> a
> array('L', [100, 2, 3])
>>>> struct.pack_into('L', a, 0, T(200))
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> struct.error: required argument is not an integer
>>>>
>
> I vastly prefer the struct module behavior. Since the code isn't executed
> by any tests:

Yeah, but if it is a good feature we can always add more tests.  I think the
real issue is whether or not this behavior is even desirable.  Also, similar
behavior can be achieved with struct by using '__index__':

...      def __init__(self, value):
...          self.value = value
...      def __int__(self):
...           return self.value
...      def __index__(self):
...           return self.value
...
>>> a = array.array('L', [1,2,3])
>>> struct.pack_into('L', a, 0, T(200))
>>> a
array('L', [200, 2, 3])

Also, check out issue1530559.  Originally, struct did allow the
'__int__' and '__long__' behavior, but it was deprecated and replaced
with '__index__'.  Maybe we should do the same for array?

IMO, having some way to convert objects to integers is a nice feature
and I think we will find more cases like the PyCUDA case from
issue1530559 where folks need this.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1172711>
_______________________________________


More information about the Python-bugs-list mailing list