|
^^^ beatenhyphz posted:Looking over a student's Python code, and see this: Well, trying to parse this out has taught me something about how Python assigns variables in for loops so that’s something. It’s dumb as hell to actually do it but not really senseless in what it’s doing. If I’m understanding right, it’s just setting the x variable in for x in y to each y as it goes through the loop. So when you’re iterating through the dictionary, this is going through the keys, and setting test["holy"] to "holy" and then "you're", which is what prints (and likewise for the lists). Everything gets modified in place because Python passes by reference. I guess you could avoid this behavior if Python passed by value rather than by reference, or kept the x variable local to the for loop, but otherwise I don’t see why it should fail to parse and run (though no one should ever actually do this, of course).
|
# ? Dec 3, 2022 19:27 |
|
|
# ? May 17, 2024 15:14 |
|
So there really is more than one way to do it?
|
# ? Dec 3, 2022 19:31 |
|
So I looked at this again and suddenly thought, hang on, if you can just change the identity of the loop control variable like that then there must be a hidden loop control value somewhere that actually tracks the position in the list. And of course there is, it's the status of the iterator created by the for loop. Which means that you can do:code:
Dijkstracula posted:I'm increasingly convinced that the world decided python was a good teaching language because somebody (rightfully) got tired of making first-year students memorise "public, static, void, main, paren, String, args, bracket, bracket paren" without understanding it, and (wrongly) not realising that complexity is just getting pushed around More because if you've had students putting stuff in classes without learning what they are, then they get deeply confused when they do learn them. But this is partly because so many courses want to teach OO using janky physical metaphors like "This desk is an object, this other desk is an object, they are both of class desk, they are different instances.." etc instead of admitting it's just type strengthening and syntactic sugar for overloaded function calls.
|
# ? Dec 3, 2022 19:40 |
|
Jigsaw posted:otherwise I don’t see why it should fail to parse and run I guess the question is “why does for…in take an arbitrary lvalue”. What use cases are there for binding anything other than an identifier or a tuple of identifiers, like we all probably assumed it did until we read that post?
|
# ? Dec 3, 2022 19:43 |
|
Dijkstracula posted:I'm increasingly convinced that the world decided python was a good teaching language because somebody (rightfully) got tired of making first-year students memorise "public, static, void, main, paren, String, args, bracket, bracket paren" without understanding it, and (wrongly) not realising that complexity is just getting pushed around needing to put all your code inside a class and an object is a coding horror all on its own though give those kids a good old fashioned int main() and then force them to grind their faces against pointers and malloc/free until they understand the difference between values and references. it builds character! oop is terrible for learning, analogies to realworld nouns are awful when you don't understand what's even being analogized. (and realworld nouns aren't good oop anyways) so is duck typing tbh. don't trust kids with a dynamic type system until they've wallowed in enough C to understand that the types still exist under the hood in python Soricidus posted:I guess the question is “why does for…in take an arbitrary lvalue”. What use cases are there for binding anything other than an identifier or a tuple of identifiers, like we all probably assumed it did until we read that post?
|
# ? Dec 3, 2022 19:58 |
|
RPATDO_LAMD posted:needing to put all your code inside a class and an object is a coding horror all on its own though
|
# ? Dec 3, 2022 20:19 |
|
hyphz posted:So I looked at this again and suddenly thought, hang on, if you can just change the identity of the loop control variable like that then there must be a hidden loop control value somewhere that actually tracks the position in the list. And of course there is, it's the status of the iterator created by the for loop. Which means that you can do: This seems pretty intuitive to me, can someone explain the horror here that my eyes aren't perceiving? The loop control variable isn't hidden, it's part of the Sequence that was created when you invoked range(). Generators work this way too: Python code:
|
# ? Dec 3, 2022 22:59 |
|
agreed, the root cause of this coding horror is that x is being mutated by both range as well as the loop body. This looks way less like a horror, imho, but is functionally the same thing:code:
|
# ? Dec 4, 2022 00:03 |
|
ultrafilter posted:So there really is more than one way to do it? Always has been, and the way that you're doing it will get deprecated in a few minor versions.
|
# ? Dec 4, 2022 01:59 |
|
Dijkstracula posted:this is why mutability is bad That doesn't seem like the best takeaway, but if you really want to only use immutable containers then tuple is right there
|
# ? Dec 4, 2022 03:49 |
|
my contribution is an angular/c# webapp with tons of typescript any and c# dynamic, courtesy of some contractor. had we not been legally required to have it go live at the beginning of the year, i would have nuked everything and started from scratch. luckily was able do that with the db schema.dougdrums posted:I feel like uuids are dumb but I don’t have any real argument to go off of devs who decide to use guids as clustered primary keys. loving may as well make your primary key a random number. champagne posting posted:Active Directory has a bunch and the Dumb of them is that different systems have different ways to identify A Thing to a Resource so that for one system you need to use a Service Principal Id while for another you'll be using an Application App Id even though you're referring to the same thing. well...yeah...but active directory is 20+ years old now. it could have been a lot worse.
|
# ? Dec 10, 2022 05:32 |
|
gnatalie posted:devs who decide to use guids as clustered primary keys. loving may as well make your primary key a random number. obligatory reminder that it's a bit more nuanced that this: in postgres primary keys are NOT clustered so guids or other randomized / distributed ids are fine in sqlite they are also not clustered unless you use WITHOUT ROWID, and if you do you hopefully know what you're doing in mssql they are clustered by default, so if you NEED sharding you should make a nonclustered pk with a guid or other shard-friendly data type, and a clustered index with a plain into in other databases, you're probably paying 5-6 figures a year for some extortionate support contract so ask them I guess
|
# ? Dec 10, 2022 20:10 |
|
ah yea i'm in the mssql world. a former dev of ours would do this whenever he could its so annoying to see, luckily there are no performance problems, compared to... ...a current dev in a completely unrelated project did this on a db with tables in the hundreds of millions / billions of rows and they ended up having to separate that into 5 (!!) servers because of huge performance problems and an inability to rewrite with a correct data structure. not my circus, not my monkeys.
|
# ? Dec 11, 2022 03:41 |
|
in sql server, you can use NEWSEQUENTIALID() to get the worst of both worlds. genius stuff
|
# ? Dec 11, 2022 06:43 |
|
Speaking of python I stumble across this the other day, code:
|
# ? Dec 11, 2022 07:13 |
|
The code literally says "I want a size-3 array, each entry should be a pointer to this object", yeah? Surprise deep-copying arrays in some situations seems like it would be an even bigger horror.
|
# ? Dec 11, 2022 07:28 |
|
Nude posted:Speaking of python I stumble across this the other day, Python sequences are always passed by-reference, so you're the one who asked for 3 references to the same object
|
# ? Dec 11, 2022 08:09 |
|
It's also something you see in other languages that have a similar object-reference model to Python. For example in C#code:
|
# ? Dec 11, 2022 21:09 |
|
I would've thought Repeat would create new objects each time, consider me schooled
champagne posting fucked around with this message at 21:39 on Dec 11, 2022 |
# ? Dec 11, 2022 21:28 |
|
This is why I love C++'s explicit references (and values by default).
|
# ? Dec 11, 2022 21:37 |
|
And why I love Swift's copy-on-write.
|
# ? Dec 11, 2022 21:41 |
|
champagne posting posted:I would've thought Repeat would create new objects each time, consider me schooled Forgive me, but I don't know why you'd have that expectation. The name doesn't suggest to me that it does that, but rather that it "repeats" the same object several times. The one-line help summary (here) says that it "generates a sequence that contains one repeated value". If anything, it's still more clear in the C# code that it'll be the same object every time, because the array is an argument to Enumerable.Repeat() and it has already been constructed when Enumerable.Repeat() starts executing. You don't really expect Enumerable.Repeat<T>() to know how to deep clone an arbitrary T? Nothing to stop you from making a method that generates a new object every time... (and of course you can do similar in Python) code:
|
# ? Dec 11, 2022 22:05 |
|
Hammerite posted:at the end of the day if you don't understand how object references work in your language it will generate plenty of surprises for you this is certainly A Take
|
# ? Dec 11, 2022 22:10 |
|
champagne posting posted:this is certainly A Take is it, now! I thought that would be a pretty uncontroversial remark.
|
# ? Dec 11, 2022 22:19 |
|
Hammerite posted:It's also something you see in other languages that have a similar object-reference model to Python. For example in C# In C# you must explicitly use the new keyword so it's obvious you're playing with references and not values. The big problem is when 'friendly' languages like Python hide all that stuff so without a deeper knowledge of the language you actually have no clue whether you're holding a reference or a copy or what until you try to mutate it and see what breaks. Basically, Hammerite posted:at the end of the day if you don't understand how object references work in your language it will generate plenty of surprises for you
|
# ? Dec 11, 2022 22:19 |
|
Hammerite posted:is it, now! I thought that would be a pretty uncontroversial remark. It just seems much harsher than you may have meant it
|
# ? Dec 11, 2022 22:21 |
|
champagne posting posted:this is certainly A Take A Correct Take e: I'm not even sure what else can be said about this, these functions/methods are doing exactly what's printed on the tin QuarkJets fucked around with this message at 22:34 on Dec 11, 2022 |
# ? Dec 11, 2022 22:32 |
|
Hammerite posted:is it, now! I thought that would be a pretty uncontroversial remark. I would consider it one of the most banal true statements you could make. There are certain things you most understand or you'll have a very bad time, and what things do and don't have reference semantics in your language is absolutely one of them.
|
# ? Dec 11, 2022 22:33 |
|
champagne posting posted:It just seems much harsher than you may have meant it ok, well if it came across as a barbed comment, it was not intended in that way.
|
# ? Dec 11, 2022 22:38 |
|
Hammerite posted:at the end of the day if you don't understand how object references work in your language it will generate plenty of surprises for you every day i pray that the offshore contractors will learn this
|
# ? Dec 12, 2022 11:14 |
|
Last time I worked with offshore contractors, they would check if key is in hashset by iterating all elements in it and compare them with the key they wanted to check. I think that's actually several steps down from "understands the reference model in language"
|
# ? Dec 12, 2022 11:30 |
|
Xarn posted:Last time I worked with offshore contractors, they would check if key is in hashset by iterating all elements in it and compare them with the key they wanted to check. I think that's actually several steps down from "understands the reference model in language" Make it work in the least efficient way possible. Make it reasonably efficient. Make it fast. You can bill for all of these. And if you do it right, you can bill for them multiple times on a single feature.
|
# ? Dec 12, 2022 14:29 |
|
RPATDO_LAMD posted:but Python makes it a lot harder to understand how references work than C# does. Python code:
|
# ? Dec 12, 2022 15:14 |
|
Ugh, yes, reference semantics wind me up. We teach languages in the order Python, Java, C/C++ and the Python and Java sessions both elide any mention of the reference model because it’s “too complicated”. Then when the students hit C’s explicit pointers they freak out completely.
|
# ? Dec 12, 2022 15:40 |
|
hyphz posted:Ugh, yes, reference semantics wind me up. We teach languages in the order Python, Java, C/C++ and the Python and Java sessions both elide any mention of the reference model because it’s “too complicated”. Then when the students hit C’s explicit pointers they freak out completely. I learned machine language, C, then a bunch of high level languages. It just hosed me up in different ways. There's no winning move for teaching this stuff.
|
# ? Dec 12, 2022 17:08 |
|
leper khan posted:I learned machine language, C, then a bunch of high level languages. It just hosed me up in different ways. A curious game
|
# ? Dec 12, 2022 17:15 |
|
Dijkstracula posted:The behaviour of `*` on lists in Python is one of my biggest footguns. Should say, though, that if one wants the behaviour that everyone expects, a list comprehension is just as succinct and will do the right thing: What kind of issues do you encounter with `*`? (I've sometimes seen this referred to as the splat operator, I like that name - and for Python it even describes the operator's behavior)
|
# ? Dec 12, 2022 17:40 |
|
QuarkJets posted:What kind of issues do you encounter with `*`? The name "splat" for * goes back at least to the original INTERCAL paper.
|
# ? Dec 12, 2022 18:12 |
|
QuarkJets posted:What kind of issues do you encounter with `*`? one of those "oh yeah that's obvious" things once you know what you're looking for, but diagnosing weird mutations that only materialise in a different part of your program is tricky
|
# ? Dec 12, 2022 19:12 |
|
|
# ? May 17, 2024 15:14 |
|
hyphz posted:Ugh, yes, reference semantics wind me up. We teach languages in the order Python, Java, C/C++ and the Python and Java sessions both elide any mention of the reference model because it’s “too complicated”. Then when the students hit C’s explicit pointers they freak out completely. my school's CS program taught ARM assembly and C in the first two semesters and made students program an rc car on a cortex m3 chip to drive around a racetrack at least when you know what a register is and wtf the difference between eax and [eax] is, it's really easy to understand C pointers
|
# ? Dec 12, 2022 20:01 |