Live data from Hacker News

Advanced Python Features

blog.edward-li.com

151–160 of 183 posts

Re: Advanced Python Features

#151
post #150

Earlier quoted context omitted.

I was merely giving an example that strong typing has nothing to do with having to write the types. (and, obviously, the inferred type (int -> int) is correct. )

Only if reveal_type only accepts an int. Just because the default value of i is 0 doesn't mean anything about what could be passed in.

not my fault python is broken.

Re: Advanced Python Features

#152
I thought the typing overload was interesting because I thought it was going to be like overloading in C# and Java but it's really just useful for documentation and intellisense tools in IDEs.

Re: Advanced Python Features

#153

Good list, its one of these things you either never heard of, or used for years and think everyone knew that. I'll add a few: * did you know __init__.py is optional nowadays? * you can do relative imports with things like "from ..other import foo" * since 3.13 there is a @deprecated decorator that does what you think it does * the new generics syntax also works on methods/functions: "def method[T](...)" very cool * y…

> did you know __init__.py is optional nowadays?

It's not optional. Omitting it gets you a namespace package, which is probably not what you want.

> TypeVar supports binding to enforce subtypes: "TypeVar['T', bound=X]",

Using the new generics syntax you mentioned above you can now do:

   def method[T: X](...)

Re: Advanced Python Features

#154
post #17

Working in the ML field, I can't hate Python. But the type system (pre-3.12, of course) cost me a lot of nerves. Hoping for a better post-3.12 experience once all libraries are usable in 3.12+. After that experience, I’ve come to truly appreciate TypeScript’s type system. Never thought I’d say that.

Hey kiddo… did you ever try something nastier? I’ve got something that will blow your mind and you’ll keep coming back You don’t know but you are addicted to types Come to the light - Haskell!

That's basically where I am coming from :). I know about my addiction.

Re: Advanced Python Features

#155
post #93

My own opinion is that Python shall remain Python, and golang, Rust and Typescript each should be whichever they are with their unique philosophy and design. I am coding in all 4, with some roughly 28 years now, and I don't like what is becoming of Python There is a reason why python become that popular and widely adapted and used, and it is not the extra layers of type checking, annotations and the likes. this looks…

I mean it's completely optional. Even if you are to use python as a glue language, with absolutely 0 typing annotations on your side, it's still very very useful to have them in the libraries that you inevitably end up using with python. It's not even like it requires an additional build step, so it's really just free.

Re: Advanced Python Features

#156
post #97
post #45

Earlier quoted context omitted.

This kind of search can be done a variety of different ways, and is worth abstracting, e.g.: def first(candidates, predicate, default): try: return next(c for c in candidates if predicate(c)) except StopIteration: return default deploy_application(first(servers, Server.check_availability, backup_server))

Instead of catching the `StopIteration` exception, you can simply provide a default case to `next` : next((c for c in candidates if predicate(c)), default)

Indeed. I was thinking of ways to generalize for the case where a default isn't desired, then decided against introducing that complexity in the example, then forgot that I could re-simplify further.

Re: Advanced Python Features

#157
post #138

Earlier quoted context omitted.

Yeah, everybody uses it. That's partly how it ends up causing so many bugs - e.g. some user puts 0 in a text box and instead of the program reacting as if you put 0 in it reacts the same way as if no value was supplied. Then everybody loses their minds because the system suddenly started doing something it was never supposed to.

I've never had this behavior cause a bug.

The most common case is when people use `if not s` to test for None in an otherwise string value (e.g. from JSON) without realizing that empty strings are also false.

Re: Advanced Python Features

#158
post #7

Earlier quoted context omitted.

I do like the else clause with for loops. However most people are not familiar with it, and also `else:` as a keyword is confusing. I always remember it as `no break:`.

I think of it as "otherwise:"

That's still confusing though. The problem here is that `else` is semantically attached to `break`, but syntactically attached to the body of the loop. The latter makes it look like it executes if the loop body didn't, if you interpret it in the most straightforward way.

IMO a better design would be to have a block that always executes at the end of the loop - there's even a reasonable keyword for it, `finally` - but gets a boolean flag indicating whether there was a break or not:

    for server in servers:
        if server.check_availability():
            primary_server = server  
            break
    finally did_break:
        if not did_break:
            primary_server = backup_server

Or better yet, make `break` take an optional argument (which defaults to `True` if unspecified), and that's what you get in `finally`. So this could be written:

    for server in servers:
        if server.check_availability():
            break server  
    finally server:
        primary_server = server if server is not None else backup_server

Re: Advanced Python Features

#159

Earlier quoted context omitted.

no need to explicitly write the type if you have type inference: > # fun x -> x + 1;; > - : int -> int = >

1) the code you wrote isn’t Python. 2) inferring the type is int isn’t guaranteed to be correct in this case

It's guaranteed to be correct if you use different operators for ints and floats, which is what at least some ML dialects (notably, OCaml) do precisely so that types can be inferred from usage.

That's the downside of operator overloading - since it relies on types to resolve, they need to be known and can't be inferred.

Re: Advanced Python Features

#160
post #45

Earlier quoted context omitted.

This kind of search can be done a variety of different ways, and is worth abstracting, e.g.: def first(candidates, predicate, default): try: return next(c for c in candidates if predicate(c)) except StopIteration: return default deploy_application(first(servers, Server.check_availability, backup_server))

Great, first() comes part of more_itertools !

`more_itertools` is definitely on my shortlist of things I wish were in the Python standard library. (The list of things I'd like to see cut out, is probably considerably longer. But that's mainly on aesthetic principles; it's probably not creating a significant maintenance burden.)

And yet somehow I keep forgetting about it in the exact moments when it would probably make my life easier.

Post reply on HN