Author meador.inge
Recipients cmcqueen1975, mark.dickinson, mattchaput, meador.inge, ocean-city, orenti, skrah, vstinner
Date 2011-09-13.02:15:39
SpamBayes Score 2.7186e-12
Marked as misclassified No
Message-id <CAK1Qooo-Ru30LP7q_f8EgMwxot7e=2gz-w+v+eP6Pxg3XETnaQ@mail.gmail.com>
In-reply-to <1315857303.94.0.891677568361.issue1172711@psf.upfronthosting.co.za>
Content
>>>> 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.
History
Date User Action Args
2011-09-13 02:15:40meador.ingesetrecipients: + meador.inge, orenti, mark.dickinson, vstinner, ocean-city, cmcqueen1975, skrah, mattchaput
2011-09-13 02:15:39meador.ingelinkissue1172711 messages
2011-09-13 02:15:39meador.ingecreate