Skip to content
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

array comprehension #4716

Closed
curly-brace opened this issue May 19, 2016 · 7 comments
Closed

array comprehension #4716

curly-brace opened this issue May 19, 2016 · 7 comments

Comments

@curly-brace
Copy link

curly-brace commented May 19, 2016

would be nice to be able to create arrays using pythonish comprehension like
array = [[0 for y in range(10)] for x in range(10)]
or even with conditionals like
array = [i for i in range(10) if i % 2]
it also enables to simulate 'map' function like
result_array = [item.count - 1 for item in other_array if item.type == 'bullet']

@Marqin
Copy link
Contributor

Marqin commented May 21, 2016

btw. pythonic list comprehensions are much faster than using map in python, so maybe there is some nice way to implement it as fast

@Calinou
Copy link
Member

Calinou commented Apr 10, 2018

First of all thank you for your report and sorry for the delay.

We released Godot 3.0 in January 2018 after 18 months of work, fixing many old issues either directly, or by obsoleting/replacing the features they were referring to.

We still have hundreds of issues whose relevance/reproducibility needs to be checked against the current stable version, and that's where you can help us.
Could you check if the issue that you described initially is still relevant/reproducible in Godot 3.0 or any newer version, and comment about it here?

For bug reports, please also make sure that the issue contains detailed steps to reproduce the bug and, if possible, a zipped project that can be used to reproduce it right away. This greatly speeds up debugging and bugfixing tasks for our contributors.

Our Bugsquad will review this issue more in-depth in 15 days, and potentially close it if its relevance could not be confirmed.

Thanks in advance.

Note: This message is being copy-pasted to many "stale" issues (90+ days without activity). It might happen that it is not meaningful for this specific issue or appears oblivious of the issue's context, if so please comment to notify the Bugsquad about it.

@bojidar-bg
Copy link
Contributor

This is still valid in master: list/array comprehensions have not been added yet.

@akien-mga
Copy link
Member

As we rejected a PR implementing this feature (#15222), I think we'll close this feature proposal as not wanted for the reasons described in the PR:

We've discussed this on IRC with many core developers, and the general feeling is that the syntax of list comprehensions makes code quite hard to read, and goes against the design principles of GDScript to have a straightfoward and easy to read syntax.

Most use cases for list comprehensions can easily be replaced with one (or nested) for loops, which are a lot more readable even if a couple lines more, so we don't see a real use case that would warrant the syntactic complexity.

See discussion on http://godot.eska.me/irc-logs/devel/2018-06-20.log from 22:10.

@Marqin
Copy link
Contributor

Marqin commented Jul 31, 2018

:/

Also I disagree thats list comprehensions are unreadable; we even had them as part of basic curriculum on Python course at University (afair it was first semester).

@nervecenter
Copy link

nervecenter commented Apr 24, 2020

Most use cases for list comprehensions can easily be replaced with one (or nested) for loops, which are a lot more readable even if a couple lines more, so we don't see a real use case that would warrant the syntactic complexity.

This is a completely ridiculous assertion. List comprehensions are declarative and describe exactly what the final data will be. Loop filling an array or list has never been as readable as a comprehension or a map function. Having to separately parse array creation, iteration bounds, conditions, and finally what is being entered into the array location (and how) is a relic of a process, a byproduct of C's dominance due to hardware limitations 40 years ago. When manipulating data, few programmers are so constrained that they think they can do better than the compiler by describing how to create an array rather than what should go in it.

You're never going to tell me that

squares_of_evens = [x**2 for x in range(10) if x % 2 == 0]

is harder to read than

squares_of_evens = []
for x in range(10):
    if x % 2 == 0:
        squares_of_evens.append(x**2)

which is noisy as all get-out.

@asluizh
Copy link

asluizh commented Apr 25, 2020

Honestly, I think not having list comprehension is more confusing, since it's such an integral part of python.

It could also allow for syntax such as
const array = [ [i for i in range(10)] for j in range(10) ]
To create constant arrays or dictionaries. Right now I have to declare it, then call a function to build it at the _ready() function, and never call it again. It seems impossible to create constant arrays with nested loops:

const array = []
for i in range(3):
   array.append(i)

Obviously, that's not constant.

This would allow us access to these constant arrays in nested classes, which is today impossible unless you pass it as argument (#4472). I understand that constant arrays is a different discussion (#32063), but I'd just like to mention that there are uses in which array comprehension syntax makes sense. Besides, of course, being pythonic (which I think is argument enough).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants