Change the array declaration syntax.
(Sorry for any spelling errors, I'm using Google translator to write this message) Lists and tuples are data structures dynamically typed, and that's great! Makes writing code easier, and allows the developer to keep the focus on more important things. However, when these structures contain large amounts of elements, or when the program requires a lot of computational resources, dynamic typingbecomes a waste if the lists and tuples not use it. One way to avoid wasting resources with dynamic typing where it is unnecessary is to use the array class. The current syntax for creating arrays is:
from array import * #necessary to create lists of single type var = array('i',[1,2,3]) #the first argument is the type and the second is the list
This class is the solution when working with lists, but there is something similar when working with tuples. Thus, the variable "var" is a list, not a tuple, even if it is declared with with a tuple instead of a list:
var = array('i',(1,2,3)) var array('i', [1, 2, 3])
I think, as well as raw strings in Python 2, lists and tuples could be declared a "single type" using a prefix.
var1 = i[1, 2, 3] # a singletype int list, as a current array var2 = i('1', '2', '3') # a singletype int tuple, as a current array, but immutable
My suggestion is to allow the type of element in the list or tuple is specified if the addition of a prefix before '[' or '('. This would simplify the use of arrays and improve the performance of programs that make use of lists of "single type". Besides creating tuples " single type". This seems more efficient, not wasting computing resources without polluting the code or make the language more complicated. For more information about the class array, the address of the documentation is http://docs.python.org/library/array.html . Thank You and Goodbye! Sig: Arthur Julião -------------------------------------------------- ---------------------------------------------- "Quero que a estrada venha sempre até você e que o vento esteja sempre a seu favor, quero que haja sempre uma cerveja em sua mão e que esteja ao seu lado seu grande amor." (Tempo Ruim - A Arte do Insulto - Matanza)
On Tue, Feb 8, 2011 at 12:52, Arthur <azrael.zila@gmail.com> wrote:
var1 = i[1, 2, 3] # a singletype int list, as a current array var2 = i('1', '2', '3') # a singletype int tuple, as a current array, but immutable
These statements are already meaningful: the first is a get-item of i with the key (1, 2, 3), the second is a function call of i with the arguments '1', '2' and '3'. This makes it unlikely the syntax will ever change this way. On the bright side, this means you can easily implement the shortcuts you want in today's Python, if you need them in a project. Cheers, Dirkjan
On Tue, Feb 8, 2011 at 9:52 PM, Arthur <azrael.zila@gmail.com> wrote:
Thus, the variable "var" is a list, not a tuple, even if it is declared with with a tuple instead of a list:
var = array('i',(1,2,3)) var array('i', [1, 2, 3])
Note that an array is its own beast - it just happens to use list notation in its repr, as the square brackets contrast better with the parentheses used for the function call syntax. Regardless, if you want a quick and easy way to create arrays of particular types, just define your own constructor function:
from array import array def iarray(*elements): ... return array('i', elements) ... x = iarray(1, 2, 3) x array('i', [1, 2, 3])
Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
I thought about how to do but did not analyze the feasibility, sorry about that. It could be after the symbol']'and ')'. So would:
var1 = [1, 2, 3]i # a singletype int list, as a current array var2 = ('1', '2', '3')i # a singletype int tuple, as a current array, but immutable
Até mais! Ass.: Arthur Julião ------------------------------------------------------------------------------------------------ "Quero que a estrada venha sempre até você e que o vento esteja sempre a seu favor, quero que haja sempre uma cerveja em sua mão e que esteja ao seu lado seu grande amor." (Tempo Ruim - A Arte do Insulto - Matanza) 2011/2/8 Nick Coghlan <ncoghlan@gmail.com>
On Tue, Feb 8, 2011 at 9:52 PM, Arthur <azrael.zila@gmail.com> wrote:
Thus, the variable "var" is a list, not a tuple, even if it is declared with with a tuple instead of a list:
var = array('i',(1,2,3)) var array('i', [1, 2, 3])
Note that an array is its own beast - it just happens to use list notation in its repr, as the square brackets contrast better with the parentheses used for the function call syntax.
Regardless, if you want a quick and easy way to create arrays of particular types, just define your own constructor function:
from array import array def iarray(*elements): ... return array('i', elements) ... x = iarray(1, 2, 3) x array('i', [1, 2, 3])
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On 2011-02-08, at 12:52 , Arthur wrote:
(Sorry for any spelling errors, I'm using Google translator to write this message)
Lists and tuples are data structures dynamically typed, and that's great! Makes writing code easier, and allows the developer to keep the focus on more important things. However, when these structures contain large amounts of elements, or when the program requires a lot of computational resources, dynamic typingbecomes a waste if the lists and tuples not use it.
One way to avoid wasting resources with dynamic typing where it is unnecessary is to use the array class. The current syntax for creating arrays is:
from array import * #necessary to create lists of single type You don't need to import * here. Array is just another type in a module.
>>> import array >>> a = array.array('i', [1,2,3]) >>> a array('i', [1, 2, 3])
var = array('i',[1,2,3]) #the first argument is the type and the second is the list
This class is the solution when working with lists, but there is something similar when working with tuples. Thus, the variable "var" is a list, not a tuple, even if it is declared with with a tuple instead of a list:
var = array('i',(1,2,3)) var array('i', [1, 2, 3])
Since 2.4, arrays can be initialized using any iterable. They're not "lists" or "tuples", they're arrays, a separate data type. You can provide an xrange as initializer if you wish to: >>> array.array('i', xrange(5)) array('i', [0, 1, 2, 3, 4]) or even provide no initializer at all: >>> array.array('i') array('i')
I think, as well as raw strings in Python 2, lists and tuples could be declared a "single type" using a prefix.
var1 = i[1, 2, 3] # a singletype int list, as a current array var2 = i('1', '2', '3') # a singletype int tuple, as a current array, but immutable Not only is this syntax ambiguous with current constructs, as Dirkjan pointed out, I don't think you've made a case for the necessity of an immutable array type so far, let alone for the necessity of a literal syntax for it (why couldn't the `array` module just have an additional type e.g. `frozenarray` similar to `set` and `frozenset`?)
My suggestion is to allow the type of element in the list or tuple is specified if the addition of a prefix before '[' or '('. This would simplify the use of arrays and improve the performance of programs that make use of lists of "single type". Besides creating tuples " single type". How would it improve the performance of code which does not currently see any need for using `array`? Would the interpreter have to infer the collection's type in order to generate the correct typed array?
The only thing one can do with a tuple that cannot be done with a list is hash it for use as a dict key. And this is the only thing that would be gained with an immutable homogeneous array type. But tuple keys are typically so short (two or three items) that there is little need for anything else. Besides which, in 3.x, bytes are immutable sequences of (small) ints and are also available as dict keys. -- Terry Jan Reedy
participants (5)
-
Arthur -
Dirkjan Ochtman -
Masklinn -
Nick Coghlan -
Terry Reedy