I don't like the distinction between "constructor" and "initializer". If you create an object without calling its __init__, chances are it's missing a bunch of instance attributes and will throw an error as soon as you try to do anything with it. Sure, technically you've created an instance of the class, but if you can't use it as a duck, is it really a duck?
Hmmm. It's early in the morning and CPython is already drunk.
>>> re.Pattern.__new__(re.Pattern)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object.__new__(re.Pattern) is not safe, use re.Pattern.__new__()
@RyanM Perhaps it helps to remember __init__ and __new__ are just hooks of calling type, same as __add__ isn't the + operator but used by it. In Python you don't call the constructor intentionally, and ClassName(...) isn't some blessed constructor syntax.
@Aran-Fey IMO the distinction helps when people want to construct something else and wonder why __init__ can't do that. It... happens...
@Aran-Fey I'm using pytest-asyncio or a self-written decorator that just forwards arguments and calls asyncio.run. Not sure why this needs a plugin, TBH.
@matszwecja Your answer could be improved by showing the equivalent of case MyCustomExceptionOne() | MyCustomExceptionTwo() since the naive equivalent – using multiple isinstances – isn't the proper way to go.
But overall I feel we are looking at an XY problem here.
From the previous discussion to that point, I'm not sure switching from os to psutil for the default count will help with k8s, but it might help with other methods of carving up instances
For example, just running a single Docker container in an instance, which you have deliberately restricted resources to. That's still going to go bonkers if you allocate 2 CPUs on a 64 core machine
Some sane default such as "4" could be enough. That shouldn't choke a 1-core allocation, still reasonably scale up on many core allocations, and not be entirely wasteful for tons of cpus.
Logarithmic scaling with number of cores also works rather well to avoid explosions from misdetecting the count.
What would that look like for poetry? It'd have to be a rapid feedback loop for it to scale in a meaningful way when it's just pulling dependencies. For that application, I think your "default of 4" makes most sense, and then people can scale it up if/when they become aware of the env var vs. watching it go boom by default and then have to find that particular setting
Ah sorry, I interpreted is as your program thinking "well 4 cores just worked fine, let's up it" during a single run. Something like a reverse backoff with a 360 twist and nailing the landing :P
I'm bored and checking out other programming languages. Any recommendations for something that's similar but preferably faster than python and doesn't have problems with circular imports?
I tried out nim recently, but the only pythonic thing about it was the syntax
They have some odds and ends that make it most suitable for data science but it's general purpose and has everything you need for, say, automation and other tasks that Python is used for.
I trust your judgment. But it's not like I have anything to do, so...
For reference, the current top contender is Typescript. The biggest turnoff is that there are some surprisingly big holes in its type system. (A method that returns -> Self? Almost impossible in TS)
Have you had a go with cython or is that insufficiently different?
That would give you a decent shot with extensions. Or maybe React Native if you're thinking of a JS slant since that would give you mobile app capabilities
Kotlin also has a shot at app dev, but I lost interest because I didn't have an immediate project to help with. Still, the language is a good improvement over Java imho
I think I'd prefer something that's separate from python. Although python's type system is surprisingly good, it still causes issues (like circular imports) that simply aren't present in languages that were designed with static typing in mind. And the same thing goes for asyncio
Well, I guess as far as asyncio goes, there weren't many languages that had it from the very beginning. But most still pulled it off better than python did
I know I'm basically looking for the ideal programming language, but 1) Rust proved that miracles can happen, 2) A man can dream and 3) I'm trying really hard to avoid adding "create a programming language" to my todo list
Anyways, I'm not aware of any real contender to Python in that regard. Most of the hard work seems to go into compiled/"proper" languages, leaving the practical stuff to the practically minded people.
Any. You spend more time pontificating about how to properly type something than I do debugging me, myself, doing something dumb. But that's me
If I get properly confused about why something I think should work but doesn't, I honestly pay zero regard to the function signature argument in terms of expected types. I just look at the logic in the function itself
Hmm. That's kind of a tough question. I guess the core of the problem is that even simple type annotations can cause issues in python, namely circular imports. So while it's arguably true that I invest too much effort into typing, investing less effort wouldn't solve my problem
And I do want to have at least simple type annotations in my code. That part's non-negotiable
Can you demo this in Vscode with a tiny example beyond basic types? I'm genuinely curious about what autocomplete can't do unless you get really meticulous
Let's say you have a function that returns a generator. You want to be able to use the generator in a with statement so that it's reliably closed even if the iteration is interrupted partway. So you write a function decorator that turns a -> Generator[T, Any, Any] function into a -> ContextGenerator[T] function. That's... slightly involved :D
I guess ints aren't the best type to test autocompletion on. But you can type i. and it should suggest as_integer_ratio(), to_bytes(), from_bytes(), etc