We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexSuggestions & BugsWebsite,  Documentation,  Book Bugs › Arrays in python comparison wrong
Page Index Toggle Pages: 1
Arrays in python comparison wrong (Read 4349 times)
Arrays in python comparison wrong
May 1st, 2005, 10:59am
 
Hello,

On the comaprison site with python you wrote:

Java:
int[] a = {5, 10, 11};
a[0] = 12;  // Reassign

Python:
a = (5, 10, 11)
a[0] = 12  # Reassign

This is wrong. x = () means a tuple in python and tuples are not changable after they are inizialized.

Let's see what happens when we try as described above:

Python 2.3.5 (#2, Mar 26 2005, 17:32:32)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
loading  .pystartup
>>> a = (1, 2, 3)
>>> a[1]
2
>>> a[0]
1
>>> a[1] = 12
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> a.__class__
<type 'tuple'>

Python does not support arrays by default. It only supports tuples, lists, and dictionaries. However there is the Numeric package for python which supports arrays .
Re: Arrays in python comparison wrong
Reply #1 - May 4th, 2005, 3:09am
 
OK. It's wrong. How can we make it right? Should we just leave the array row out of the comparison or would it be more useful to explain the difference?
Re: Arrays in python comparison wron
Reply #2 - May 4th, 2005, 12:55pm
 
in the context of the comparison and main use scenarios of how arrays are used in processing/java, maybe it's safe to compare them to python lists (at least in this context). they share same functionality to array of other dynamically typed languages like actionscript/javascript or lingo:

Code:
a=[5,10,11] # create a list, not a tuple
a[0]=12 # reassign an index
a.append(23) # grow list
a.pop() # remove the last element

maybe it's also worth noting python lists are closer to Java's Vector or ArrayList classes. unlike the "primitive" array types they can contain mixed types of data and have no fixed length...
Re: Arrays in python comparison wrong
Reply #3 - May 5th, 2005, 1:18pm
 
Lists provide the same functionality (in this context) like arrays in java. However if you want to do heavy gfx coding in python you will probably use the Numeric package (http://www.numpy.org/). Here is an example with it: http://phuturetech.info/code/?path=./python/ifsFractal2.py&action=showfile
Re: Arrays in python comparison wrong
Reply #4 - May 6th, 2005, 8:44pm
 
OK. It's updated now. Please have a look. I tried to balance accuracy and brevity. Thanks to both jc-denton and toxi for the precision.

http://processing.org/reference/compare/python.html
Page Index Toggle Pages: 1