this post was submitted on 10 Nov 2024
48 points (90.0% liked)

Technology

59381 readers
4205 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related content.
  3. Be excellent to each another!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, to ask if your bot can be added please contact us.
  9. Check for duplicates before posting, duplicates may be removed

Approved Bots


founded 1 year ago
MODERATORS
top 9 comments
sorted by: hot top controversial new old
[–] thebestaquaman@lemmy.world 17 points 6 days ago (3 children)

While I do agree with most of what is said here, I have a hangup on one of the points: Thinking that "docstrings and variable names" are a trustworthy way to indicate types. Python is not a statically typed language - never will be. You can have as much type hinting as you want, but you will never have a guarantee that some variable holds the type you think it does, short of checking the type at runtime. Also, code logic can change over time, and there is no guarantee that comments, docstrings and variable names will always be up to date.

By all means, having good docstrings, variable names, and type hinting is important, but none of them should be treated as some kind of silver bullet that gets you around the fact that I can access __globals__ at any time and change any variable to whatever I want if I'm so inclined.

This doesn't have to be a bad thing though. I use both Python and C++ daily, and think that the proper way to use Python is to fully embrace duck typing. However that also means my code should be written in such a way that it will work as long as whatever input to it conforms loosely to whatever type I'm expecting to receive.

[–] troyunrau@lemmy.ca 4 points 5 days ago (1 children)

Duck typing is the best if fully embraced. But it also means you have to worry just a little bit about clean failures once the project grows a little. I like this better than type checking relentlessly.

It also means that your test suite or doctests or whatever should throw some unexpected types around now and again to check how it handles ducks and chickens and such :)

[–] thebestaquaman@lemmy.world 4 points 4 days ago (1 children)

I have to be honest in that, while I think duck typing should be embraced, I have a hard time seeing how people are actually able to deal with large-scale pure Python projects, just because of the dynamic typing. To me, it makes reading code so much more difficult when I can't just look at a function and immediately see the types involved.

Because of this, I also have a small hangup with examples in some C++ libraries that use auto. Like sure, I'm happy to use auto when writing code, but when reading an example I would very much like to immediately be able to know what the return type of a function is. In general, I think the use of auto should be restricted to cases where it increases readability, and not used as a lazy way out of writing out the types, which I think is one of the benefits of C++ vs. Python in large projects.

[–] troyunrau@lemmy.ca 3 points 4 days ago

Generally speaking, I like duck typing for function inputs, but not as much for function outputs (unless the functions are pure mathematics).

[–] muntedcrocodile@lemm.ee 6 points 5 days ago

I just type hint all my functions and that solves 99% of my type related issues.

[–] sugar_in_your_tea@sh.itjust.works 3 points 5 days ago* (last edited 5 days ago) (1 children)

I can access __globals__ at any time

Not on my team you can't.

[–] thebestaquaman@lemmy.world 3 points 4 days ago

Watch me: My void* doesn't give a shit about your const!

[–] turkalino@lemmy.yachts 9 points 5 days ago (1 children)

The “but muh types” criticism of Python has always been completely ignorant of the main use cases of Python. Like yeah, probably shouldn’t use it in avionics or medical devices. But scientific computing where you’re basically just using the interpreter as a calculator? You really only care about whether something’s an integer or not. Float vs. double isn’t gonna kill you

We use optional types everywhere for our server code, and that works really well. Not sure what if not variable: means? Just look at its type, no big deal. We don't annotate everything, but we annotate enough that static analysis tools can tell us what the type of pretty much any variable is. And most of the time, it's not even necessary because the variables are clear enough that the type can be inferred.

So yeah, not an issue.