Message143948
>>>> 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. |
|
Date |
User |
Action |
Args |
2011-09-13 02:15:40 | meador.inge | set | recipients:
+ meador.inge, orenti, mark.dickinson, vstinner, ocean-city, cmcqueen1975, skrah, mattchaput |
2011-09-13 02:15:39 | meador.inge | link | issue1172711 messages |
2011-09-13 02:15:39 | meador.inge | create | |
|