Is Python now the default language?

iljitsch

Ars Tribunus Angusticlavius
9,637
Subscriptor++
So many caveats...

One thing that continues to annoy me is that if I say:

a = [ ]

and then for

a[20] = "something"

or

a["something"] = 20

I get an error. I'm sure that there are worlds where this makes sense, but why oh why make me spend cycles trying to figure that out? Just give me what I want.

If strong typing is really necessary, I can deal with that. But either have that or get out of my way. Pretending to have weak typing and then give me type errors is insanity.

And I can deal with C-type arrays. Or untyped associative arrays. But Python seems to want to provide the worst of both worlds: you don't get to define an array size, but you also don't get to use arbitrary names as array indexes. What??

And I could be completely wrong about any and all of the above. But the documentation doesn't clear that up. It's all five layers of indirection. Yes, that works in programs, but my brain can't handle it for documentation. Just give me concrete examples of what to do.
 

Pino90

Ars Tribunus Militum
1,715
Subscriptor
You're confusing a list with a dictionary. These are two different things. You can't access a list with a string. You can access a dict with a number or a string. That's it.

Why you would want to use a string to index a list is beyond my comprehension. Maybe it's something other languages let you do, but it doesn't seem like a good idea.
 
  • Like
Reactions: kaced and bjn

Pino90

Ars Tribunus Militum
1,715
Subscriptor
Okay, trying to be more useful.

a[20] = "something"

Not a typing problem, that's just how lists work. A list is an ordered sequence and index 20 doesn't exist yet. The fix is either append() or just initializing it with the right size. This is actually consistent with most programming languages. You can't access arr[20] in C if you only allocated 5 elements either, you just get undefined behavior instead of an error.

For the other case you just want a dict.

a = {}
a["something"] = 20
# this works just fine

If you want php arrays just use a dict.
 
  • Like
Reactions: continuum

bjn

Ars Praefectus
5,633
Subscriptor++
What Pino said. You really need to read up on Python basics rather than type randomly and hope it works. The language really isn’t that hard.

Code:
list = [] # make an empty list
list.append(10) # add ten to it
anotherList = [1, 2, 3, 4] # make a new populated list
dic = {} # empty dict
dic[“a”] = 1 # add a value to the dict
anotherDict = {“a”:1, “b”:2} # make a populated dict

I’m happy to teach you more if you pay me my contracting rates.

Dammit, ninja’d
 
Last edited:

Pino90

Ars Tribunus Militum
1,715
Subscriptor
Tbf @iljitsch you should just read a tutorial. In 2 hours you'd probably be up to speed. A language reference documentation is not really a tutorial, it's just... The reference docs. You're learning by collision and by somehow expecting that python behaves like either C or PHP while it clearly doesn't.

docs.python.org/3/tutorial is... Decent.

Otherwise you could read this (free) book which is actually great.

https://automatetheboringstuff.com/

ETA since we're here, if you ask me virtual environments are kinda very important and you should be confident with them before your runtime turns to crap. Using poetry or similar dependency manager is also very recommended.

Venv to create a virtual env, project.toml for the dependencies and then poetry install / poetry update. There are a tons of tools for this exact use case and YMMV. IIRC some users in the other threads prefer other alternatives.
 

w00key

Ars Legatus Legionis
10,097
Subscriptor
Or talk to an LLM. Gemini is a free and patient teacher. Tell it you want to learn Python in a hurry, like Neo downloading a skill in the Matrix. It even works for more complex skills like Unity.

114529.jpg
 

kaced

Wise, Aged Ars Veteran
115
Subscriptor
a = [ ]

and then for

a[20] = "something"

or

a["something"] = 20

I get an error.
You can’t sensibly do this in C either. Like, you can, but it won’t do what you want. In C, if you define a pointer and treat it like an array, you can write to a[20] but you shouldn’t unless you are pointing at a buffer of the right size, right?

So you need to define the size of your array first. If you want it to be 20 items long:

a = [0] * 20

Now every element in it is initialized to 0. You can extend it later if you need. But you probably want to do that from the start! Why should it be 20 long if nothing is in it yet?

If you want it to be multidimensional, where each element is a list, yeah ok I see how it’s slightly annoying to define it compared to the concision of C. But often you should do it differently. It’s rare you really need that. Python has good ways to do things that aren’t like C. It will likely be clearer to the reader (who could be yourself a month from now) if you use a dictionary or something else. Similarly with the 20 element list: if you are assigning a mental value to what it means to be in cell 20 vs 19, rather than it simply being a list of things in order, you probably want some other data structure. Probably a dict.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
So many caveats...

One thing that continues to annoy me is that if I say:

a = [ ]

and then for

a[20] = "something"

or

a["something"] = 20

I get an error. I'm sure that there are worlds where this makes sense, but why oh why make me spend cycles trying to figure that out? Just give me what I want.

If strong typing is really necessary, I can deal with that. But either have that or get out of my way. Pretending to have weak typing and then give me type errors is insanity.

And I can deal with C-type arrays. Or untyped associative arrays. But Python seems to want to provide the worst of both worlds: you don't get to define an array size, but you also don't get to use arbitrary names as array indexes. What??

And I could be completely wrong about any and all of the above. But the documentation doesn't clear that up. It's all five layers of indirection. Yes, that works in programs, but my brain can't handle it for documentation. Just give me concrete examples of what to do.
So, this is just strong typing at work, you created an empty array using a literal so it behaves like an array, meaning that, if you want to store something in the 20th slot, you must first resize it. And, of course, because it's an array, it requires array semantics, which means indexes are numbers. If you want an associative array, you need to use the literal for that which is bound in {} and idea : to separate keys from values.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
If you want it to be multidimensional, where each element is a list, yeah ok I see how it’s slightly annoying to define it compared to the concision of C.
Given that you have to initialize it in C, it's not particularly different. Though with a list comprehension, you should be able to make it more concice. Something like:

Python:
matrix = [[0] * width for _ in range(height)]
 

iljitsch

Ars Tribunus Angusticlavius
9,637
Subscriptor++
You're confusing a list with a dictionary. These are two different things. You can't access a list with a string. You can access a dict with a number or a string. That's it.

Why you would want to use a string to index a list is beyond my comprehension. Maybe it's something other languages let you do, but it doesn't seem like a good idea.
Why would I want to spend cycles learning the difference?

I'm not here to bow down to programming languages spending time to learn their principles, they're here to make my life easier. If they don't, GTFOOH.

I can't do:

a = []
a[ "something" ] = "something"

I can't do:

a = [(whatever, like 10)]
a[10] = 10

So now how am I supposed to store stuff in arrays/lists/dictionaries for which I don't know the full set of indexes in advance?

I'm absolutely sure a universe exists where this makes sense.

But why on earth should I spend time figuring out how to work around all of this? There are so many programming languages. If they all require me to spend a large amount of time learning their particulars, that means I won't be using pretty much any of them. The job of a programming language is to make simple stuff simple and complex stuff possible. Python is not entirely succeeding here.
 

dmsilev

Ars Tribunus Angusticlavius
8,002
Subscriptor
I can't do:

a = []
a[ "something" ] = "something"
But you can do
Python:
a={}
a["something"] = "something"
That generates a dictionary containing a single key-value pair.

As far as I can tell, you seem want [] to be overloaded to refer to both arrays and key-value dictionaries. That way madness lies. Does any language do that?
 
  • Like
Reactions: kaced and bjn

Pino90

Ars Tribunus Militum
1,715
Subscriptor
Why would I want to spend cycles learning the difference?

I'm not here to bow down to programming languages spending time to learn their principles, they're here to make my life easier. If they don't, GTFOOH.

I can't do:

a = []
a[ "something" ] = "something"

I can't do:

a = [(whatever, like 10)]
a[10] = 10

So now how am I supposed to store stuff in arrays/lists/dictionaries for which I don't know the full set of indexes in advance?

I'm absolutely sure a universe exists where this makes sense.

But why on earth should I spend time figuring out how to work around all of this? There are so many programming languages. If they all require me to spend a large amount of time learning their particulars, that means I won't be using pretty much any of them. The job of a programming language is to make simple stuff simple and complex stuff possible. Python is not entirely succeeding here.
For like the 12th time in this thread, use an effing dictionary.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
Why would I want to spend cycles learning the difference?

I'm not here to bow down to programming languages spending time to learn their principles, they're here to make my life easier. If they don't, GTFOOH.

I can't do:

a = []
a[ "something" ] = "something"

I can't do:

a = [(whatever, like 10)]
a[10] = 10

So now how am I supposed to store stuff in arrays/lists/dictionaries for which I don't know the full set of indexes in advance?

I'm absolutely sure a universe exists where this makes sense.

But why on earth should I spend time figuring out how to work around all of this? There are so many programming languages. If they all require me to spend a large amount of time learning their particulars, that means I won't be using pretty much any of them. The job of a programming language is to make simple stuff simple and complex stuff possible. Python is not entirely succeeding here.
Because this isn't some "particular to Python" feature we are discussing here. Instead Arrays and (vs) Maps / Dictionaries / hashes are fundamental data structures which exist in pretty much every programming language and have implications for how you wrote code in every language from Java to Ruby to C to JavaScript. Honestly, I can't think of a language where this is not fundamental.* Once you learn it you know what tool to look for in whatever language you happen to be using.

Also, in Python you can do a["foobar"]

Python:
dict = {}
dict["foobar"] = "baz"
Works just fine.

* Edit: And apparently I know little about PHP, which apparently doesn't have arrays because making iteratign through a list an expensive memory intensive operation is apparently fine.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
I can't do:

a = [(whatever, like 10)]
a[10] = 10

So now how am I supposed to store stuff in arrays/lists/dictionaries for which I don't know the full set of indexes in advance?
Also, the same way you do in pretty much any language with arrays: either ask the array how long it is or resize it to be as long as you need it to be. It's an array, the full set of indexes is range(0, size - 1).*

* Edit: something that can be said of arrays in pretty much every programming language save those that have 1 indexed arrays, where the full set of indexes is range(1, size). If you can index an array with anything else, that's because it isn't an array.

Edit2: fixed off by one error.
 

Pino90

Ars Tribunus Militum
1,715
Subscriptor
Unless your mental model is damaged by PHP or Lua. Both have a mixed hash and array type, where array is a hybrid dense and sparse array and mapping, not a full fat pre-allocated chunk of certain size. You can do a = [] (or a = {} in Lua), a[184782938] = "hi" without massive allocation.
Of course. But then complaining that Python behaves like every other popular programming language ever designed is... weird?
 
  • Like
Reactions: bjn

hanser

Ars Legatus Legionis
43,847
Subscriptor++
Why would I want to spend cycles learning the difference?

I'm not here to bow down to programming languages spending time to learn their principles, they're here to make my life easier. If they don't, GTFOOH.
This is the dumbest fucking thing I've read today, and I spent like an hour on Twitter earlier.

So now how am I supposed to store stuff in arrays/lists/dictionaries for which I don't know the full set of indexes in advance?

I'm absolutely sure a universe exists where this makes sense.
As everyone has shouted at you, dictionaries. Just talk to a fucking LLM already so we don't have to read your bad ideas.

But why on earth should I spend time figuring out how to work around all of this? There are so many programming languages. If they all require me to spend a large amount of time learning their particulars, that means I won't be using pretty much any of them. The job of a programming language is to make simple stuff simple and complex stuff possible. Python is not entirely succeeding here.
Data structures and algorithms are A Thing in CS and programming for a reason. Being mad about it doesn't change the fundamentals.

I don't normally gatekeep, like, anything but also... what the fuck.
 

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
Unless your mental model is damaged by PHP or Lua. Both have a mixed hash and array type, where array is a hybrid dense and sparse array and mapping, not a full fat pre-allocated chunk of certain size. You can do a = [] (or a = {} in Lua), a[184782938] = "hi" without massive allocation.
That's more of a consequence of weak typing and Lua hiding what's going on under the hood for you. Suffice it to say that initializing an array with twenty elements and saying a[20] = 10 might not do the same thing. If you want to take an array and turn it into a map in such languages you can say a["foo"] = bar and the language will copy the entire array into a dictionary before adding "foo" to the list of keys. Suffice it to say that doing so has preformance implications, which means you still need to understand the difference between an array and a map. (Javascript is similar in that regard).
 

Haas Bioroid

Ars Tribunus Militum
1,792
Subscriptor
Unless your mental model is damaged by PHP or Lua. Both have a mixed hash and array type, where array is a hybrid dense and sparse array and mapping, not a full fat pre-allocated chunk of certain size. You can do a = [] (or a = {} in Lua), a[184782938] = "hi" without massive allocation.
Isn't Javascript also faulty of that ? Or am I mistaken with any other Javascirpt trazillion cases of bad design ?
 

bjn

Ars Praefectus
5,633
Subscriptor++
Why would I want to spend cycles learning the difference?

I'm not here to bow down to programming languages spending time to learn their principles, they're here to make my life easier. If they don't, GTFOOH.

Python is a stupidly easy programming language to learn and you've spent more time whining on here than it would have taken to get your head around the basics of dicts and lists.

I can't do:

a = []
a[ "something" ] = "something"
No, because a list isn't a dictionary, they are distinct things as they are in most sane programming languages and/or libraries. Python's lists and dicts have very similar semantics to the equivalents in every other programming languages I know, eg: std::vector & std::map in C++, vec and hashmap in Rust, List and Map in dart.

I can't do:

a = [(whatever, like 10)]
a[10] = 10

So now how am I supposed to store stuff in arrays/lists/dictionaries for which I don't know the full set of indexes in advance?
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a.append(10)

Easy peasy.

I'm absolutely sure a universe exists where this makes sense.

But why on earth should I spend time figuring out how to work around all of this? There are so many programming languages. If they all require me to spend a large amount of time learning their particulars, that means I won't be using pretty much any of them. The job of a programming language is to make simple stuff simple and complex stuff possible. Python is not entirely succeeding here.
Programming is inherently complicated, different languages are designed to make certain things easier than others*. Pick one appropriate to the what you are trying to achieve and get on with it. This may require more than 5 seconds of trying to understand the language in question. So your "I can't be arsed to learn one of the most widely used and east to learn programming languages in the world because it doesn't work the way I expect it to!" indicates to me a lack of seriousness on your behalf.


*Well, except for Javascript, Javascript wasn't actually designed at all, brainfuck has clearer design principles than JS.
 

kaced

Wise, Aged Ars Veteran
115
Subscriptor
Why would I want to spend cycles learning the difference?

I'm not here to bow down to programming languages spending time to learn their principles, they're here to make my life easier. If they don't, GTFOOH.

…

But why on earth should I spend time figuring out how to work around all of this? There are so many programming languages. If they all require me to spend a large amount of time learning their particulars, that means I won't be using pretty much any of them. The job of a programming language is to make simple stuff simple and complex stuff possible. Python is not entirely succeeding here.
I…you…what??? You don’t want to spend 5 minutes learning the difference between the widespread concept of an array vs. a map/hash table (Python list vs dict), and this is Python’s fault, and now the fault of everyone trying to explain it to you? I don’t think we can help you here. I have many complaints about Python myself, no doubt! Basic syntax covered in the first page of any tutorial is not one of them.

Why on Earth would you learn how a language works? Well, let’s see, maybe in order to use it? If you don’t like Python or the 90% of other languages that work this way, ok don’t use it, that’s fine. Yes, there are many languages available in this century, so maybe one of them does this specific unusual thing you want.
 
Last edited:

fitten

Ars Legatus Legionis
55,632
Subscriptor++
Why would I want to spend cycles learning the difference? [between lists and dictionaries]
Well... dictionaries (maps, or in general 'hash tables') are widely used in programming. I've used them so much over the years I don't think I could live without them for long ;) In fact, back in my Perl days (early 200X years when I was writing a lot of Perl) I was using them so much that I wrote my own C hash table to use in my own projects. I found this subject interesting because I watched this video just this morning...

 
  • Like
Reactions: bjn

kaced

Wise, Aged Ars Veteran
115
Subscriptor
Well... dictionaries (maps, or in general 'hash tables') are widely used in programming. I've used them so much over the years I don't think I could live without them for long ;) In fact, back in my Perl days (early 200X years when I was writing a lot of Perl) I was using them so much that I wrote my own C hash table to use in my own projects. I found this subject interesting because I watched this video just this morning...


Yeah, it’s funny that this whole argument seems to boil down to “arrays aren’t maps” when dicts are one of the best parts of Python. I have to stop myself from using dicts sometimes because it’s so convenient. It’s true, maps are great! Every language should totally have them easily accessible! Just like Python!
 
  • Like
Reactions: bjn

Apteris

Ars Tribunus Angusticlavius
9,589
Subscriptor
I found this subject interesting because I watched this video just this morning...
Egad, that video is cancer. No offence, thank you for the new information, it's cool, but that particular explanatory video (something I am a big fan of in general) is in the "thanks, I hate it" category.

For the record, the paper is Optimal Bounds for Open Addressing Without Reordering - Martin Farach-Colton, Andrew Krapivin, William Kuszmaul, and its key insight (as explained to me by ChatGPT, I'm not too proud to admit) is that it is possible to consider the existing structure of the hash table -- what space is occupied, and what space is free -- and arrange insertions strategically so as to get dramatically better lookup performance as occupancy increases, at a non-prohibitive cost to insertions.

Again, I watch a lot of YouTube, for videos both educational and not, but in this particular case I would recommend that the interested reader glance at the paper and then ask someone (or something, I guess) to explain the more intricate parts.
 
  • Like
Reactions: fitten

Lt_Storm

Ars Tribunus Angusticlavius
21,343
Subscriptor++
Isn't Javascript also faulty of that ? Or am I mistaken with any other Javascirpt trazillion cases of bad design ?
It's possible to do that kind of thing in any sufficiently powerful weakly typed language. But, it would take quite a bit of custom code to make JavaScript behave that way, you would likely need to redefine the Array type at runtime (or, more sensibly, use something else). Hell, it should be possible in C++*, though I doubt it would yield sensible or worthwhile results there. Not that it's likely to yield worthwhile results in JavaScript, again, if you have the need, just don't use an array.

* Yes, C++ is a weakly typed language. Static typing, it turns out, isn't the same things as strong typing. Languages can be statically and weakly typed (C++), statically and strongly typed (Java, Haskell), dynamically and weakly typed (Lua, Javascript), or dynamically and strongly typed (Python, Ruby, Lisp, Smalltalk). Though, with dynamic binding, you can do many things that look weakly typed in Python, Ruby, Lisp, and Smalltalk.
 

Haas Bioroid

Ars Tribunus Militum
1,792
Subscriptor
It's possible to do that kind of thing in any sufficiently powerful weakly typed language. But, it would take quite a bit of custom code to make JavaScript behave that way, you would likely need to redefine the Array type at runtime (or, more sensibly, use something else). Hell, it should be possible in C++*, though I doubt it would yield sensible or worthwhile results there. Not that it's likely to yield worthwhile results in JavaScript, again, if you have the need, just don't use an array.

* Yes, C++ is a weakly typed language. Static typing, it turns out, isn't the same things as strong typing. Languages can be statically and weakly typed (C++), statically and strongly typed (Java, Haskell), dynamically and weakly typed (Lua, Javascript), or dynamically and strongly typed (Python, Ruby, Lisp, Smalltalk). Though, with dynamic binding, you can do many things that look weakly typed in Python, Ruby, Lisp, and Smalltalk.
Preach brother, I’m a F# fanboy.