Live data from Hacker News

I'm switching to Python and actually liking it

cesarsotovalero.net

511–520 of 718 posts

Re: I'm switching to Python and actually liking it

#511
post #482

Earlier quoted context omitted.

This is nitpicking, but this is a good usecase for the := operator: if not (API_KEY := os.getenv("API_KEY")): ... For internal tools I just let os.environ["API_KEY"] raise a KeyError. It's descriptive enough.

no one uses walrus

[dead]

Re: I'm switching to Python and actually liking it

#512

> Not only because the syntax is more human-friendly, but also because the Python interpreter is natively integrated in all Unix distros That's kind of very optimistic evaluation - literally anything beyond "import json" will likely lead you into the abyss of virtual envs. Running something created with say Python 3.13.x on Ubuntu 22.04 or even 24.04 (LTSs) / Rocky 9 and the whole can of worms opened. things like vir…

this is solved by uv

uv really is working super super super hard to absolve decades of sins and blood in the python world. and doing a good job at redemption.

Re: I'm switching to Python and actually liking it

#513

Earlier quoted context omitted.

Even slightly better is to first check both if not API_KEY and not CHANNEL_ID: print("Missing both YOUTUBE_API_KEY and YOUTUBE_CHANNEL_ID.") exit(1) if not API_KEY: print("Missing YOUTUBE_API_KEY.") exit(1) if not CHANNEL_ID: print("Missing YOUTUBE_CHANNEL_ID.") exit(1) That way you don't end up fixing one just come back and be told you're also missing another requirement

Even better would be to only check each once and buffer the decision: valid = True if not API_KEY: print("Missing YOUTUBE_API_KEY.") valid = False if not CHANNEL_ID: print("Missing YOUTUBE_CHANNEL_ID.") valid = False if not valid: exit(1) This way you only check each value once (because your logic might be more complicated than just checking it's not set, maybe it can be wrongly formatted) and you still get to do wha…

[dead]

Re: I'm switching to Python and actually liking it

#514

Earlier quoted context omitted.

Well that explains why you think pip is broken

Because I went to the official pip "getting started" docs and did exactly what it says? It's bad even in venv though, like not managing your requirements.txt or installing conflicting stuff. The web is full of questions about this, with answers that involve installing some other thing.

pip freeze

but yes, pip predates the paradigm of package managers managing your dependency file instead of you managing your dependency file and then invoking the package manager

Re: I'm switching to Python and actually liking it

#515

Earlier quoted context omitted.

Java shops are certainly where I've witnessed the most disdain for Python. IME the strongest feelings tend to come from people who didn't actually have any significant experience with Python, and perhaps don't even have much practical experience with any language that isn't Java. So they tended to consider it to be objectively inferior purely because it's interpreted and dynamically typed. At a previous job I did man…

> Java, for its part, can also accumulate a lot of dynamic language-style performance losses to pointer chasing and run-time type lookups (and GC churn) if you're not careful about how you use generics. The worst thing about Java is the average quality of Java programmer.The same could probably be said about Python. However I think that there are fewer Python programmers trying to write AbstractFactoryFactory than in…

I certainly think so but wasn’t sure if it was just my unfamiliarity or lack of advanced programming knowledge but I attempted what seemed like a very simple patch to a Java project (guacamole) and it was insane how I ended up having to add the new function to like a base and abstract class and interface etc. it was crazy. All the same boilerplate too.

Re: I'm switching to Python and actually liking it

#516

Maybe I'm the only one that finds Python simultaneously verbose and lacking? Either you need 500 dependencies to do something in a simple way, or you need dozens (if not hundreds) of lines to do trivial things. I avoid writing Python because there's so much bullshit to add. Much prefer Perl, I can actually get things done quickly. Python feels like programming for the sake of programming.

> Much prefer Perl, I can actually get things done quickly Python lets you just nest data structures without having to twist your brain. You want a tuple in a list in a dictionary value: you just write it down and can access it with a unified notation. Bam. No reference madness and thinking about contexts. It's a big part of what I typically need to get things done quickly and understand how I did it 5 years later. T…

That's what got me off Perl. I loved it and was skeptical about Python, but one time after trying it for a week or so, I wondered what it would look like to pass a dict of lists of dicts into a function, then reference items inside it. My first try worked: I made a dict, with a list inside it, and a dict inside that list, and passed it as an argument without any sigils or reference decorators or anything.

That was about the time I stopped using Perl for any new projects. I never wanted to go back.

Re: I'm switching to Python and actually liking it

#517
post #319

Earlier quoted context omitted.

This isn't really unique to the walrus operator, it's just a general python quirk (albeit one I find incredibly annoying). `for i in range(5): ...` will leave `i` bound to 4 after the loop.

Oddly enough, "except" variables don't remain bound! try: x = int('cat') except Exception as e: pass print(e) # So, it appears Python actually has three variable scopes (global, local, exception block)?

Exception blocks don't create a different scope. Instead, the name is explicitly (well, implicitly but deliberately) deleted from the scope after the try/except block runs. This happens because it would otherwise produce a reference cycle and delay garbage collection.

https://stackoverflow.com/questions/24271752

https://docs.python.org/3/reference/compound_stmts.html#exce...

Re: I'm switching to Python and actually liking it

#518
post #429

Earlier quoted context omitted.

Neophytes take notice. Attention to details like this is what separates truly great programmers from merely good ones. That said, for scripts reusable by others you should use command line arguments . Environment variables in lieu of command line arguments is a huge code smell.

You don't generally want API keys accidentally recorded into someone's bash history.

What exactly is your threat model, where the attacker can read ~/.bash_history but can't execute (or capture output from) /usr/bin/env?

Re: I'm switching to Python and actually liking it

#519

Maybe I'm the only one that finds Python simultaneously verbose and lacking? Either you need 500 dependencies to do something in a simple way, or you need dozens (if not hundreds) of lines to do trivial things. I avoid writing Python because there's so much bullshit to add. Much prefer Perl, I can actually get things done quickly. Python feels like programming for the sake of programming.

Could you share an example, where Perl is quicker to use and more powerful?

Using it as a replacement for Bash. No environment required, Perl is already installed on every computer on earth. Perl is generally quicker to write for string-oriented workflows - which is what Bash is. Regex is quick and easy.

Perl falls apart when you need structure and even small projects. For short scripts meant to do what you would use Bash for, it's a godsend. Safer, more portable, less friction.

Re: I'm switching to Python and actually liking it

#520

Earlier quoted context omitted.

> it's default This point is true; the ecosystem simply can't change overnight. uv is getting there, I hope. > it always works the same everywhere `uv` works the same, everywhere? > it actually writes down the deps `uv add` does that, too. > I don't have to manually switch between them (or set up fancy bashrc triggers) like venvs. You don't have to do that with `uv`, either?

Hope they make uv default then. It's nice, but I have to separately create a project with it, and regular python commands don't work with it, both of which go back to it not being default. But even that won't fix all the old projects.

node is not the default in js projects either, it's just the currently most popular manager. Old JS projects are their own bundle of fun.
Post reply on HN