-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java arrays are Iterable, but aren't Sequence #1252
Comments
I believe that when I wrote the wrapper I attempted to make arrays and lists into sequences, but was unable to do so. The issue was that Java arrays did not satisfy certain requirements to be a sequence and we would need to add a utility class to add those additional portions of the contract. Look at the complete contract required for sequence. It would be possible to create a large wrapper to add that much functionality but it certainly wouldn't be normal Java behaviors. https://docs.python.org/3/library/stdtypes.html#id4 What specific behavior of Sequence do you think we need to support? |
It didn't pass on mind the need to satisfy the whole Sequence contract!
IMO, Java arrays already satisfy what's actually important to be considered a Sequence. These few operations & methods below fail: concatenated = python_tuple + java_array
doubled = java_array * 2
print(java_array.count(30))
print(java_array.index(20)) However, everything else works: print(20 in java_array, 25 not in java_array) # True True
sliced = java_array[2:4]
print(sliced, type(sliced)) # [30, 40] <java class 'int[]'>
INV = slice(None, None, -1)
inverted = java_array[INV]
print(inverted, type(sliced)) # [50, 40, 30, 20, 10] <java class 'int[]'>
print(len(inverted), min(inverted), max(inverted)) # 5 10 50
I just wanna be able to declare a parameter of type Sequence which would accept many types of indexed containers, including Python lists, tuples, etc.; and Java containers such as arrays, arraylists, etc. |
I will give a shot in 1.6 to see if I can get the missing part of the contract to work and it to recognize it. It will depend a lot on whether Sequence is a duck type or just a declaration. If it is a declaration then we can satisfy without completing the contract. |
I have this test which does some simple checks on Java's arrays and arraylists & Python's arrays and tuples:
While an ArrayList is both an Iterable and a Sequence, a Java's array is just an Iterable, but not a Sequence!
Given a Java's array is also subscriptable and we can use Python's
[]
operator w/ positive integer indices, I believe a Java array should also be considered a Sequence in Python, just like an ArrayList.The text was updated successfully, but these errors were encountered: