Issue 686323: Minor array module enhancements (original) (raw)

Created on 2003-02-14 01:13 by phr, last changed 2022-04-10 16:06 by admin. This issue is now closed.

Messages (5)

msg53760 - (view)

Author: paul rubin (phr)

Date: 2003-02-14 01:13

  1. I notice that array('B', (2,3,4)) throws an error saying the 2nd arg must be a list or string. That is, array('B', [2,3,4]) is legal but using the tuple (2,3,4) is not. The limitation doesn't seem harmful, but I also don't see any good reason for it. May as well remove it.

  2. Of more usefulness (and maybe of more controversy), I think the byte array methods should accept string arguments, for example a = array('B', 'abc') a.append('def') should do the obvious thing. That gives a natural way to build up a string in pieces instead of making a list of strings and doing the counterintuitive ''.join(x) maneuver. Appending to byte arrays is the usual way to build up a string in Java, if that matters. So I favor this change too.

msg53761 - (view)

Author: Raymond Hettinger (rhettinger) * (Python committer)

Date: 2003-02-14 04:34

Logged In: YES user_id=80475

The first request is reasonable.

The second is not the natural meaning of append(). In python, multiple appends are handled with extend(). The array module already provides extend. So, for the example given above, the code is:

a = array('B', 'abc') a.extend(array('B', 'def')) a array('B', [97, 98, 99, 100, 101, 102])

msg53762 - (view)

Author: paul rubin (phr)

Date: 2003-02-14 04:42

Logged In: YES user_id=72053

You're correct, second request should be for extend rather than append.

That is,

a = array('B', 'abc') a.extend('def')

should do the obvious thing.

msg53763 - (view)

Author: Raymond Hettinger (rhettinger) * (Python committer)

Date: 2003-04-24 10:48

Logged In: YES user_id=80475

Added the first request as arraymodule.c 2.88

I'm passing on the second request for now. Auto-promoting the argument entails attempting to construct a new array object of the same type as that being extended. That is a bit cumbersome but doable. It also requires that any array creation errors be trapped and re-explained the context of array.extend().

msg53764 - (view)

Author: Raymond Hettinger (rhettinger) * (Python committer)

Date: 2004-03-14 05:53

Logged In: YES user_id=80475

Implemented the second request as a form of array.extend().

Given, a=array('c', 'the quick'), you can now write, a.extend(' brown fox').

See Modules/arraymodule.c 2.95