Live data from Hacker News

I'm switching to Python and actually liking it

cesarsotovalero.net

701–710 of 718 posts

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

#701
post #627

Earlier quoted context omitted.

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...

This is the type of things that make me roll my eyes at all the wtf JavaScript posts[0], yes there are a lot of random things that happen with type conversions and quite a few idiosyncrasies (my favourite is that document.all is a non empty collection that is != from false but convert to false in an if) But the language makes sense at a lower level, scopes, values, bindings have their mostly reasonable rules that are…

> yes there are a lot of random things that happen with type conversions and quite a few idiosyncrasies... the language makes sense at a lower level, scopes, values, bindings have their mostly reasonable rules

The "random things" make it practically impossible to figure out what will happen without learning a whole bunch of seemingly arbitrary, corner-case-specific rules (consider the jsdate.wtf test currently making the rounds). And no, nobody is IMX actually simply complaining about NaNs existing (although the lack of a separate integer type does complicate things).

Notice that tests showcasing JavaScript WTFery can work just by passing user data to a builtin type constructor. Tests of Python WTFery generally rely on much more advanced functionality (see e.g. https://discuss.python.org/t/quiz-how-well-do-you-know-pytho...). The only builtin type constructor in Python that I'd consider even slightly surprising is the one for `bytes`/`bytearray`.

Python's scoping is simple and makes perfect sense, it just isn't what you're used to. (It also, unlike JavaScript, limits scope by default, so your code isn't littered with `var` for hygiene.) Variables are names for objects with reference semantics, which are passed by value - exactly like `class` types in C# (except you don't have to worry about `ref`/`in`/`out` keywords) or non-primitives in Java (notwithstanding the weird hybrid behaviour of arrays). Bindings are late in most places, except notably default arguments to functions.

I have no idea what point you're trying to make about __add__; in particular I can't guess what you think it should mean to "inspect" the method. Of course things work differently when you use the C API than when you actually write Python code; you're interacting with C data structures that aren't directly visible from Python.

When you work at the Python level, __add__/__iadd__/__radd__ implement addition, following a well-defined protocol. Nothing happens "at creation time"; methods are just attributes that are looked up at runtime. It is true that the implementation of addition will overlook any `__add__` attribute attached directly to the object, and directly check the class (unlike code that explicitly looks for an attribute). But there's no reason to do that anyway. And on the flip side, you can replace the `__add__` attribute of the class and have it used automatically; it was not set in stone when the class was created.

I'll grant you that the `match` construct is definitely not my favourite piece of language design.

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

#702
post #80

Just a small note on the code in the linked script: API_KEY = os.environ.get("YOUTUBE_API_KEY") CHANNEL_ID = os.environ.get("YOUTUBE_CHANNEL_ID") if not API_KEY or not CHANNEL_ID: print("Missing YOUTUBE_API_KEY or YOUTUBE_CHANNEL_ID.") exit(1) Presenting the user with "Missing X OR Y" when there's no reason that OR has to be there massively frustrates the user for the near zero benefit of having one fewer if statemen…

Thank you. I see something like this all the time on one of the sites I use for work. If you fail the 2-factor, it'll tell you your password was wrong and reset the whole thing instead of telling you the 2-factor code was wrong or expired.

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

#703
post #395
post #287

> I would like to have a tool that generates the project structure for me, but I haven’t found one that fits me yet. I recommend cookiecutter for this. I have a few templates I've built with that which I use frequently: python-lib: https://github.com/simonw/python-lib click-app: https://github.com/simonw/click-app datasette-plugin: https://github.com/simonw/datasette-plugin llm-plugin: https://github.com/simonw/llm-p…

Am I the only one who actually likes setting up new projects? I don't want to automate that.

I’m legitimately curious about what you enjoy and why. Rewriting boiler plate code and trying to ensure that I get everything right with a process I don’t often execute is the very definition of toil to me.

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

#704
post #680

Earlier quoted context omitted.

uv supports the standardised lock file format. This means that you're not tied to uv for anything, as most project settings it uses (eg metadata, dependencies, extras, etc.) are based on PEPs. poetry has its own format for many things. https://peps.python.org/pep-0751/

Ah, I see that this is a newer format than the projects I've been using poetry for. Are there advantages to move to it?

Interoperability being the major one. You can switch between uv and other tools (such as pdm and hatch) with minimal effort.

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

#705
post #483

Earlier quoted context omitted.

why print then exit(1) instead of raising an exception?

Oh, I know this one: $ python3 -c "print('clear messaging'); exit(1)" clear messaging $ python3 -c "raise ValueError('text that matters')" Traceback (most recent call last): File " ", line 1, in ValueError: text that matters and that story gets _a lot_ worse when some programs raise from within a "helper" module and you end up with 8 lines of Python junk to the one line of actual signal

[deleted]

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

#706
post #395

Earlier quoted context omitted.

Am I the only one who actually likes setting up new projects? I don't want to automate that.

I’m legitimately curious about what you enjoy and why. Rewriting boiler plate code and trying to ensure that I get everything right with a process I don’t often execute is the very definition of toil to me.

It allows me to gain a basic understanding of the fundamentals of my code base.

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

#707
post #80

Just a small note on the code in the linked script: API_KEY = os.environ.get("YOUTUBE_API_KEY") CHANNEL_ID = os.environ.get("YOUTUBE_CHANNEL_ID") if not API_KEY or not CHANNEL_ID: print("Missing YOUTUBE_API_KEY or YOUTUBE_CHANNEL_ID.") exit(1) Presenting the user with "Missing X OR Y" when there's no reason that OR has to be there massively frustrates the user for the near zero benefit of having one fewer if statemen…

Just use

  if not API_KEY:
    sys.exit("Missing YOUTUBE_API_KEY.")

It will print message to sys.stderr and do system exit with code 1.

P.S. exit() is just an alias to sys.exit(), I prefer longer form.

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

#708
post #689
post #250

Earlier quoted context omitted.

>Except it doesn't. That is only true if you never reexamine the universality of your statement. I promise that it is possible to "solve" the mess that was Python's ecosystem, that uv has largely done so, and that your preconceptions are holding you back from taking advantage of it.

Here's the thing: there has never been a lack of people who have declared this problem as solved in the 20 or so years since Python started Poking its way into my professional life. (And for about 12-13 years before that I could gladly ignore it since nobody did much of anything in it). People have said this since the days of the Blackberry. Multiple times people have explained why they think whatever they are madly…

I would take a look at uv adoption. That's what makes it different. It nails everything that all the other tools have done and it does it fast. So it's been what people have been using for a while now. Even poetry never seemed to get this ubiquitous of support.

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

#709
post #627

Earlier quoted context omitted.

This is the type of things that make me roll my eyes at all the wtf JavaScript posts[0], yes there are a lot of random things that happen with type conversions and quite a few idiosyncrasies (my favourite is that document.all is a non empty collection that is != from false but convert to false in an if) But the language makes sense at a lower level, scopes, values, bindings have their mostly reasonable rules that are…

> yes there are a lot of random things that happen with type conversions and quite a few idiosyncrasies... the language makes sense at a lower level, scopes, values, bindings have their mostly reasonable rules The "random things" make it practically impossible to figure out what will happen without learning a whole bunch of seemingly arbitrary, corner-case-specific rules (consider the jsdate.wtf test currently making…

> methods are just attributes that are looked up at runtime

At runtime when evaluating a + b no dunder method is looked up and there is no guarantee that a + b === a.__anydunder__(b) https://youtu.be/qCGofLIzX6g

What i mean with weird scoping is

    def foo():
      e = 'defined'
      try:
        raise ValueError
      except Exception as e:
        print(e)
      print(e) # this will error out

    foo()
I also dislike how local/global scopes work in python but that is more of a personal preference.

I agree that that Javascripts standard library is horrible the jsdate.wtf is an extreme but apt example, IMO most of these are solved with some "defensive programming" but I respect other opinions here.

> And no, nobody is IMX actually simply complaining about NaNs existing

I watched many Javascript WTF! videos on youtube and NaNs and [2] == "2" were usually 90% of the content.

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

#710
post #709

Earlier quoted context omitted.

> yes there are a lot of random things that happen with type conversions and quite a few idiosyncrasies... the language makes sense at a lower level, scopes, values, bindings have their mostly reasonable rules The "random things" make it practically impossible to figure out what will happen without learning a whole bunch of seemingly arbitrary, corner-case-specific rules (consider the jsdate.wtf test currently making…

> methods are just attributes that are looked up at runtime At runtime when evaluating a + b no dunder method is looked up and there is no guarantee that a + b === a.__anydunder__(b) https://youtu.be/qCGofLIzX6g What i mean with weird scoping is def foo(): e = 'defined' try: raise ValueError except Exception as e: print(e) print(e) # this will error out foo() I also dislike how local/global scopes work in python but…

Anyway actually my biggest gripe with python is that i find the module/impor/export system counterintuitive
Post reply on HN