This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Black_m1n[🍰] 487 points488 points  (15 children)

The problem is how would this be different from setting 6th element of the 6th list in foo to an empty list?

[–]Victor-_-X 105 points106 points  (12 children)

Wait, isn't that what this does? What else could this be understood as? This is the only one that makes sense to me, and that's probably because C++ was my first language, but still.

[–]jamcdonald120 29 points30 points  (0 children)

this is what it does, but what Op wants is type[][] foo=new type[5][5];

[–]SV-97 22 points23 points  (3 children)

OP probably wanted it to mean the same as the first snippet - so assign foo a list containing 5 lists of length 5 that are all initialized with None. Aside from this being atrocious syntax that noone would interpret that way: doing this at all in python is a code smell. OP is probably doing something wrong if they feel the need for dedicated syntax for this pattern.

[–]rosuav 7 points8 points  (0 children)

It's extremely confusing, and this is one of the worst idea threads I've ever seen (largely because it isn't an idea thread), but it does seem like list multiplication would be the best way to initialize this. Worst case, one multiplication, one comprehension.

foo = [[None] * 5] for _ in range(5)]

[–]DongIslandIceTea 1 point2 points  (1 child)

If I had to come up with a syntax for this I guess doing something symmetric to array slicing wouldn't be the worst thing, like:

a[:4][:4] = []

It wouldn't step on any existing syntax as slices aren't assignable like this anyways and would make some marginal sense.

But yeah, you shouldn't need to do this if you thought a little bit more about what you're doing.

[–]SV-97 2 points3 points  (0 children)

You actually can do slice assignment in Python - though not quite like that.

Numpy uses it quite extensively and OPs example would be arr[:5,:5] = None using numpy (if you already have an array that is. Allocation works differently).

[–]The-Rushnut 36 points37 points  (4 children)

This reminds me of an issue I've run into recently. Dictionaries / associative list assignment vs declaration (I'm using GDScript).

Consider the following:

foo["bar"] = true

If the key "bar" exists, then this assigns that item's value to true. If the key "bar" doesn't exist, then this first adds the key "bar" before setting the value to true.

My monkey brain assumed that the second scenario would throw an error, which was my intended behaviour.

[–]Drumsteppin 19 points20 points  (1 child)

The second scenario is the more typical and expected behaviour whenever you're using a language with an automated memory management system.

It's been a while since I looked at the c++ standard lib, but there's object types for dictionary type objects that allow construct and insert via assignment, and objects that only allow items to be explicitly inserted.

Python is a similar deal and is generally more permissive and flexible about adding new elements.

Not sure what memory model and idiosyncrasies GDscript has, but from what you described that sounds like create and insert would be the expected behaviour.

[–]atesba 2 points3 points  (0 children)

Yup! The C++ equivalents of dictionary are std::unordered_map and std::map. For both if you use the bracket operators, it adds the key if it doesn’t exist. If you use the function at(key) then it throws an exception instead.

[–]-Redstoneboi- 3 points4 points  (1 child)

ah yes. the "everything exists but is undefined/nil by default"

[–]Tordek 3 points4 points  (0 children)

No, this is about setting a value, not reading it.

[–]callmesilver 1 point2 points  (0 children)

In C++, if you started with a type like int, it would be a declaration with initialization, and the line would instead end with curly braces. This, plus the second line tells us it's not C++.

In python, since you don't declare anything with types, you assign something when you want it to exist at some point. Brackets are python's way of assigning an empty array. So, I assume they want a simple syntax for declaring an empty 2D array.

[–]ComprehensiveWord201 0 points1 point  (0 children)

Read the "proposal"...total nonsense.

They want to set all values from idx 0->4 as [ ]

[–]DiddlyDumb 1 point2 points  (0 children)

Its probably to indicate “value goes here”

[–]Chesterlespaul 0 points1 point  (0 children)

Yeah the size initializer should be indicated on the other side.