20 ms·
Writing Python like it's Rust
- autorun 3y agoA nice library to handle results like Rust is https://github.com/rustedpy/result https://github.com/rustedpy/result
- SOLAR_FIELDS 4y agoTooling has come a long way in the past few years to help support better Python. Nowadays I can just throw on Pydantic V2 (still in Alpha, but stable to code against for basic use cases), ruff and mypy. Turn strict mode on in mypy and install the relevant vscode extensions and you get an experience that rivals a compiled language in both speed and usability.
- fayalalebrun 4y agoWhat do you do when your dependencies don't have types? Also, with speed do you mean compilation or runtime?
- LtWorf 4y agoHe means he didn't benchmark it against a compiled language :D :D :D
- SOLAR_FIELDS 4y agoI mean compilation time, obviously ;)
- LtWorf 4y agoIn my experience, due to not needing a plugin (and not having the completely not checked BaseSettings), typedload is better in respect to runtime type safety and using mypy. I designed it keeping mypy in mind from the beginning.
- Yoric 4y agoIn terms of usability, I feel that getting a Python codebase to a point where typing can actually be relied upon requires much, much, much more work than writing the same code in Rust or any other language with strong, static typing. And mypy is fragile enough that you can fairly easily sneak code that will break the guarantees without any warning, so even when I write "typing can actually be relied upon", I feel I'm overselling it. YMMV
- LtWorf 4y agoYou are completely disregarding the fact that python has a lot of modules. In rust you need an external dependency for anything. And of course external dependencies can be unmaintained, have vulnerabilities, change API every week…
- Yoric 4y agoGood point. Anecdotally, though, I have the opposite experience. Python libraries changing API under my feet regularly (without support from the type system to help me find out how), core features that I depend on not being available in any maintained library and vulnerabilities. It is entirely possible that my experience is biased, of course. Out of curiosity, can you think of a module that is part of Python's standard distribution and is not available in Rust as a well-maintained crate?
- deleted 3y ago[deleted]
- nailer 4y ago> Is records a list, a dict or a database connection? Records is a list of record items. It’s a plural. If it was a dict, it would be recordByID. If it was a database connection it would be called connection. Strict typing is good, and you should definitely use it, but you should still have good names.
- vsnf 4y ago>If it was a dict, it would be recordByID. Never have I ever encountered any codebase where dictionaries were so named.
- paganel 4y agoI usually add a “_dict” to the variable name.
- psd1 4y agoI follow that convention. I've written many functions that accept a list of values and, typically as intermediate processing, build a dict from them. Many rest APIs I've coded for require me to fetch from two endpoints to build my output, and this pattern saves iterating over lists.
- mgaunard 4y agoMost good databases actually store data per column. So you'd have a list of batches, each batch being a dict of string to list of T.
- AdityaSanthosh 4y agoSo cool. I love rust and my day job is in Python. But I have a question. I'm a junior dev(gimme some leeway here). I don't really understand how important these design patterns are because in the programs I write, I usually write the classes and call them in runtime myself. I think usually we write the servers and client ourselves. Let's take the different client types example. You are making an assumption that users can call close on a closed client. Is it so hard to just follow the sequence ` client = Client() client.connect() client.authenticate() client.send_message() client.close() ` Aren't they overengineering? Perhaps I have not worked in a large code bases to understand the problems TypeState Pattern or more generally these design patterns solves. I understand that these patterns are elegant and make the future modifications or enhancements easier but I have never seen the tangible value enough in real life
- atoav 4y agoI think it depends™ – when you know for sure there will just be one place to call this from, your approach is totally okay and wrapping it in seperate types would be overkill. The seperate types will come in handy, once you end up using that client object throughout the codebase and you are not sure anymore who is connected, who is authenticated and so on. By pushing that onto the type system you can make less mistakes.
- biorach 4y ago> Perhaps I have not worked in a large code bases to understand the problems Basically, yes. In a large codebase the steps in your example could well be separated by thousands of lines of code, with much branching, or perhaps complicated inheritance hierarchies. In that case leveraging type hints to avoid logic errors can avoid a lot of hard-to-spot bugs
- dalai 4y agoDepends on what you do with the Client. If you have a complicated application and pass the Client around between different functions then maybe you would need to check every time whether the client is connected and authenticated before sending a message. If the function only accepts an authenticated client, then the type checker will complain. You could also notice the problem in a unit test, but it may be hard to represent all possible states. In simpler situations where every time you just do the sequence you probably would want to combine some of these calls anyway.
- okl 4y agoType hints are a great feature in Python to enhance readability and to catch programming errors when refactoring or learning a new library. A lot of the features presented here are helpful for programming in the large, i.e., once the program exceeds mental capacity. As a general goal I think that programmers should reduce complexity even of simple stretches of code so that, adding all its context, a larger chunk of code fits into their brain at once and errors become more visible. Part of that is unloading tasks onto the tools. Unfortunately, its still common practice to create write-only code, often justified by an "it works" (usually not true).
- mgaunard 4y agoI've seen a bunch of code like this, usually written by juniors once they get a bit of experience and discover the concept of types. A surefire way to make your Python code terrible. The whole point of Python is the duck typing. If you're not going to use that then you might as well use a real programming language.
- silviot 4y agoCan you enumerate the languages that you deem "real" and those you don't? I have a very hard time understanding what meaning you give to that word.
- mgaunard 4y agoLanguages that you would build serious production software in, rather than some script. C, C++ are the main ones.
- linkdd 4y agoSo you deny that the industry used Python to build serious production softwares? Understood, no need to argue then, I don't think any amount of proof would change your mind...
- mgaunard 4y agoI also do not consider websites or smartphone apps to be serious software, despite the huge amount of work that happens to be done there. You know the rule, 99% of everything is crud.
- iudqnolq 4y agoI find it hard to understand how you can believe something like Dropbox isn't "serious production" just because it involves websites and apps.
- 4y ago
- quietbritishjim 4y ago> Dataclasses instead of tuples or dictionaries The point is good in general, but it's still perfectly possible to use tuples and get all the field name and typing benefits: just use typed named tuples [1]. Unfortunately this is buried in the typing module. I couldn't even find it in the table if contents, and I knew what I was specifically looking for. https://docs.python.org/3/library/typing.html#typing.NamedTuple https://docs.python.org/3/library/typing.html#typing.NamedTu...
- raverbashing 4y ago> typed banned tuples I think you mean typed named tuples
- quietbritishjim 4y agoOops, thanks! Fixed now
- deleted 4y ago[deleted]
- Kwpolska 4y agoNamed tuples, typed or not, are a bad idea for most data structures. They might make some sense when dealing with coordinates (x, y, z), but why would you want to use it for an Employee class, as shown in the example? This is a tuple, so you can iterate over it, employee[0] == "Guido", and employee[1] == 3. This isn’t useful in any way, it’s confusing, and it’s making it harder to change the order of fields in this class. Just use an actual class, not a class pretending to be a tuple sometimes.
- quietbritishjim 4y agoI wasn't saying that you always ought to use a named tuple instead of a dataclass. Only that, if you do want a tuple, that option is still available. Personally, I would use a tuple if I want something immutable. I realise you can do that with dataclasses by setting frozen=True but it feels a little over engineered to me.
- throwaway290 4y agoThe thing with type hints in Python is that it's not enforced unless you make and maintain checking it as part of your workflow. And it comes with a bunch of things, at least: make sure to configure mypy, make sure you run mypy after push (experienced pythonists will forget to run it before), make sure everyone's IDE has it integrated, make sure to be super clear on which version of mypy you run (so errors people see in their IDE are the exact same as on CI). And don't forget to update all this when new version comes out.
- crosser 4y agoHaving mypy in the pre-commit hook helps a lot! (But I agree that difference between mypy versions is a pain.)
- jstx1 4y agoIt's not that many steps. You already have CI - adding mypy is one extra line, you should already have a way to keep track of versions in your project - keeping track of the mypy version isn't any additional overhead. If your team uses VSCode, you can have a devcontainer for the project (https://code.visualstudio.com/docs/devcontainers/containers https://code.visualstudio.com/docs/devcontainers/containers) to make sure that it's in everyone's IDE along with your other linters, formatters etc and you can also have pre-commit hooks.
- throwaway290 4y ago> You already have CI Don't worry about me. I mean all projects that do not have people and resources for this stuff. People just want to write Python and be done with it. Without type hints they wrote Python and if they run it and tests pass then it works. Now they write Python code that can be wrong but they don't find out unless they also run an extra tool. The fact that it requires CI now is a good illustration of the problem;)
- jstx1 4y agoI don't understand who the "just want to write Python and be done with it" demographic is. If you're a developer, it makes sense to do things well, if you aren't and you're writing small one-off scripts, you can do fine without type hints. Anyone who can write and run a test can also call "mypy ." on their project.
- dcferreira 4y agoCool post, I've also been using more and more type hints and it really makes writing python more enjoyable. Although unfortunately it's still quite common for libraries to not have type stubs. On the section of construction functions, I've always used/seen `@classmethod` instead of `@staticmethod`. I just had a quick look at some big python libraries (pandas, transformers), and they do use class methods. Is there any particular reason you'd want to use static methods instead?
- extr 4y agoFWIW personally I use @staticmethod when I have small-ish "utility" methods that are only ever used in-class. Basically just a convenient way of organizing code and making the intended context clear.
- oefrha 4y agoFactory methods should be class methods if the class is meant to be inherited from. Since most classes people write day to day aren’t going to be inherited from, ever, the distinction is irrelevant for the most part. I doubt they have a particular reason for choosing the less extensible option.
- kobzol 4y agoI don't think that it's a good idea in general to inherit the construction functions. But if you wanted to do that, classmethod is the way to go, of course.
- extr 4y agoWhat is the smart money doing for type checking in Python? I've used mypy which seems to work well but is incredibly slow (3-4s to update linting after I change code). I've tried pylance type checking in VS Code, which seems to work well + fast but is less clear and comprehensive than mypy. I've also seen projects like pytype [1] and pyre [2] used by Google/Meta, but people say those tools don't really make sense to use unless you're an engineer for those companies. Am just curious if mypy is really the best option right now? [1] https://github.com/google/pytype https://github.com/google/pytype [2] https://pyre-check.org/ https://pyre-check.org/
- arcanemachiner 4y agoI just did some highly unscientific spelunking on the topic a couple hours ago, and my takeaway was more or less that a bunch of people on reddit said pyright was better.
- oofsa 4y agoYou may already know this, but Pyright and Pylance are the same thing. "Under the hood, Pylance is powered by Pyright," https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance https://marketplace.visualstudio.com/items?itemName=ms-pytho... I've been using Pylance with `"python.analysis.typeCheckingMode": "basic"` for a long time and have found it quite good. Most of the time, the problem isn't Pylance/Pyright, but poor or wrong type annotations in third-party libraries.
- ptx 4y agoMaybe they produce the same results (I don't know), but Pylance using Pyright doesn't mean that Pylance is Pyright. One important difference in this case is that while "Pylance leverages Microsoft's open-source static type checking tool, Pyright" [1], Pylance itself is not open source. In fact, the license [2] restricts you to "use [...] the software only with [...] Microsoft products and services", which means that you are not allowed to use it with a non-Microsoft open source fork of VS Code, for example. The license terms also say that by accepting the license, you agree that "The software may collect information about you and your use of the software, and send that to Microsoft" and that "You may opt-out of many of these scenarios, but not all". [1] https://github.com/microsoft/pylance-release https://github.com/microsoft/pylance-release [2] https://marketplace.visualstudio.com/items/ms-python.vscode-pylance/license https://marketplace.visualstudio.com/items/ms-python.vscode-...
- livinglist 4y agoI started my programming journey with C# which is a statically typed language, and when I later was introduced to Python when I was in college, I really liked how convenient it is but the lack of typing made me really uncomfortable.
- jstx1 4y agoThere's typing in Python. Strong typing even. It's just that it's also dynamic.
- hakanderyal 4y agoI went through a similar journey, without the Rust part. Started using type hints, data classes, pydantic to get the benefit of static typing after dealing with the pain of refactoring dynamically typed projects. It was better, but it _feels_ like lipstick on a pig. I love Python, but types are not what it's best at. It's missing features that makes typing easier. I've realized if was going to write typed Python, I might as well switch to one that has better support for that and switched to .NET. I can do almost all the dynamics things that I can do in Python with C#, but in a type safe way. Expressions and extension methods, reflection with `nameOf` opens up a lot of possibilities. One thing that kept me using Python was SqlAlchemy, an ORM, a damn good one. Entity Framework Core, after V6 is a better one, especially with LINQ. If you have the opportunity, give it a try. You might be surprised.
- JodieBenitez 4y ago> One thing that kept me using [language X] was [insert awesome library here] And that's why I keep [language X], because sometimes the equivalent in [language Z] is not as good.
- Kwpolska 4y agoIf [awesome library] is the primary thing your code does, then sure, you might be stuck with [language X]. But if it isn’t, then perhaps the [language Z] replacement might be good enough (or better but not yet well understood, as in the case of the OP), or you might keep [language X] just for that small component.
- tejinderss 4y ago> I can do almost all the dynamics things that I can do in Python with C#, but in a type safe way. If you like flexibility with a good type system, give Typescript a try, its miles ahead of mypy and its designed by same person who’s behind C#.
- AlexSW 4y agoI don't think this is a completely accurate comparison, as TypeScript is transpiled to JavaScript and this provides restrictions to the expressibility and usability of types at runtime, which I've sometimes hit my head against. C# has much more type information available at run time, as does Python.
- cies 4y agoSee how much discipline is needed to "write Python as Rust"? This is the real problem: you can have great code in dynamic/liberal/forgiving languages, but it will be due to programmer's discipline. Using a tool like Rust (or other strict/strongly-typed languages) forces some quality constraints on all code that compiles. This is, to me, a great benefit of these languages.
- ouija 4y agoYes, you can write Python code that runs with missing or wrong type hints. Not ideal, but you can add a static type checker (mypy) as a step in your CI pipeline and reject commits that fail this step. Not much discipline required.
- Too 4y agoYou need even more discipline to write code without the type hints. So a move in the right direction.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- hakuseki 4y agoThe `find_item` example uses List for an argument. To my thinking, this indicates that the function is intended to mutate the argument. I don't think that was the author's intention, though, so I would prefer to use Sequence in this situation (or possibly Iterable, if we only need to traverse the sequence once in order).
- kobzol 4y agoSomeone else also mentioned this on Reddit, good point.
- deleted 4y ago[deleted]
- BiteCode_dev 4y agoLots of comments here are stating that typing is half baked in Python, and that if you gotta use types, you should use another language. But that's missing the point that Python is still not meant to be the best at anything, but good at most things. And in this case, it's exactly what you get: optional typing, with decent safety if you need it. You can quick script or design seriously, you can explore in a shell or commit a public API into a file, you can start with untyped code and add some later. That makes it cumbersome sometimes, yes. mypy is slow, and the typing system + match / case have many rough edges. But it also keeps Python incredibly versatile. It's amazing what you can do with this (not so) little language, and the whole ecosystem is always getting better and better. Every time I try another language, I keep getting back into Python, because the fields I need tools for are vast, and it's the only one that I'm pretty sure will handle a problem decently in all it's various forms, especially the one I didn't consider when I started. There is immense value in this, because it keeps opportunities on the table no matter what you do. I often find my self during a project adding something I could only because Python let me do so easily. Python is quirky, yet it still damn practical.
- timkofu 4y ago> Lots of comments here are stating that typing is half baked in Python, and that if you gotta use types, you should use another language ... I thought this is how most of us use Python? I find it repulsive to use without types. type_hints enable me to use it for Software Engineering, and I get to keep the Data Science stack, which is solid gold.
- tialaramex 4y ago> the fields I need tools for are vast, and it's the only one that I'm pretty sure will handle a problem decently in all it's various forms Whilst I agree with this stance to some extent, we should be clear that Python is not in fact the panacea you've made it out to be here. It is very possible that a particular problem requires performance Python can't match, so that any Python solution will be too big/ slow/ clumsy and must be rewritten in a better language. It is also very possible that a particular problem requires safety Python can't match, so that any Python solution will be too dangerous and must be rewritten in an appropriate safe language to avoid unacceptable losses.
- tmarice 4y agoThis is a great way to to write very unaesthetic Python.
- ansgri 4y agoIt's almost like with Python you have a spectrum of aesthetics and maintainability. The "most aesthetic" (concise and readable-as-plain-English) code involves a lot of magic (metaclasses, dynamic dispatch etc.), so it's better left to foundational libraries with extensive testing by thousands of developers. Except they probably have a ton of asserts and type hints under the hood, so only the interface is "aesthetic". And if you just want to write a maintainable code, it's less work to just stick to simpler and uglier constructs with explicit types.
- Yoric 4y agoYou are right, that feels like a pretty good description of the tradeoffs when writing Python. While I'm not a big fan of Go, I kinda understand why Go doesn't allow writing "aesthetic" libraries, because the cost in terms of behind-the-scenes maintenance is enormous.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- LeonardoTolstoy 4y agoFunny. I just started a side personal project and decided, since I didn't use types much at work to just try out a bunch of the newer python features. My code came out pretty much exactly like the blog. But I've never coded in Rust. I could have written the exact same blog (excepting maybe the last bit) and called it "writing Python like it's Scala".
- drekipus 4y agoI love python, and I understand the importance of typing, but is there a way to have less physical keyboard typing? Consider the two examples: `private Dictionary<RoadPlate, List<RoadPlate>> road = new Dictionary<RoadPlate, List<RoadPlate>>();` `road : dict[RoadPlate, List[RoadPlate] = {}` Or even just `road = {}` I prefer python simply due to needing less writing and less reading. I can think more about the program when there's less stuff to read
- Yoric 4y agoMany years ago, I used to code in OCaml. I used to think exactly like you (OCaml has extreme levels of type-inference, so you write as little code as Python and you get type safety). However, as I worked on larger codebases, I realized that most of these type hints on which I was saving a few keystrokes were actually part of the documentation. When I returned to older code, it looked smart, too smart, but I had no clue about my invariants. And in your particular example, if you write `road = {}`, chances are that it will end up being typed as a `Dict[str, Any]`, which is basically worthless, both to avoid TypeError, but also as a suggestion of what you should and shouldn't put in the dictionary. These days, I write my types aggressively.
- rami3l 4y agoIndeed, that has been a real problem, so now you can probably write `private Dictionary<RoadPlate, List<RoadPlate>> road = new();`.
- drekipus 4y agoThis was my main complaint, as I nlk no one the dict part needs typing, but surely `new` could infer what I want. If so, I'm very glad.
- zelphirkalt 4y agoAt some point you gotta ask yourself: Why am I still writing Python then, if I want to write Rust? If I want Rust's safety, why not write Rust then, with battle proven tools and better type system from the start? Often the answer to this question unfortunately is not a technological merit, but knowledge of a team and willingness to learn. You might want to write actual Rust code and there can be any number of benefits, but if your team does not want to learn Rust, you got a problem and cannot move on. Here it becomes visible, how important it is to learn multiple programming languages. It makes teams flexible and allows choosing technologically better solutions, instead of having to shoehorn things into that one language the team knows. Programming languages matter. Each has its strong areas and flaws. Some languages allow you to change the language itself, to extend what you can do with them comfortably and safely. All this is assuming of course, that you will have suitable replacements for any libraries you need. Such replacements can including writing some code oneself, using the language's facilities. For example, if the language is powerful enough or includes the right batteries for it, you can easily write a little parser for a file format yourself using a grammar that is available in some standard. But keep maintainance in mind. If the file format is still developing, you might not want to do this. Something that in one language might be an external dependency, a library, can in another language be a concise self-written code, that one can look at and quickly understand.
- eska 4y agoIn my case I write Python like this because the colleagues who are using my software are hardcore engineers but only intermediate or beginner programmers. Well-designed APIs with IDE support and safe patterns protect them from a lot of potential mistakes they didn’t know they could make. I won’t be able to ask them to learn Rust out of time constraints.
- orthoxerox 4y ago> At some point you gotta ask yourself: Why am I still writing Python then, if I want to write Rust? If I want Rust's safety, why not write Rust then, with battle proven tools and better type system from the start? Garbage collection
- 4y ago
- DANISHS 4y ago[flagged]
- nimmer 4y agoThe author should really look at Nim.
- deleted 4y ago[deleted]
- mrweasel 4y agoIsn't that more equivalent of "I really want to have a typed and compiled language, but I'd also like to keep the Python like syntax". Nim is an underrated language, it's easy to pick up and plenty fast.
- esafak 4y agoThe language is dandy, but it needs better tooling. If I wanted to support the project, that's where I'd direct my energy.
- kobzol 4y agoI will someday :) But unless there's Tensorflow and about a gazzillion other packages in Nim, it's not easy to switch.
- davidatbu 4y agoYou know: this always tempts me, but I hold back because nim is not memory-safe (or has a clearly delineated memory-safe subset like Rust), and I would rather pick up an actually memory-safe (even if GCed) language even if some perf-cost (like F#, Ocaml, Swift, Vale ..etc).
- cb321 4y agoThere is a fairly clear subset of Nim which is memory safe. There are discussions once in a while to reduce that from a small set to one..two keywords, e.g.: https://forum.nim-lang.org/t/9280#61243 which has links to some others. The resolution is usually: Eh - it's already a very small set. There also may well be libs (that you feel unable to not rely upon) which use these language constructs, and trust sure is tricky (in any PL ecosystem as well as just in life).
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- bmacho 4y agoCan't we write and read Rust like Python instead?
- mrweasel 4y agoThat would be better, I dislike much of the syntax in Rust, but I do love many of the concepts and the overall goals. I don't know, typing in Python is something I do to get code completion to work in VSCode. Mostly I don't care for it, because I frequently find myself using duck typing to solve problems. Python is really good at doing what I mean and not bothering me with "technically you're wrong"... I don't care, the function does what's it's suppose to, I have tests.
- JodieBenitez 4y agoRust is not pretty, but it's not that bad: use actix_web::{get, web, App, HttpServer, Responder}; #[get("/hello/{name}")] async fn greet(name: web::Path<String>) -> impl Responder { format!("Hello {name}!") } #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new().service(greet) }) .bind(("127.0.0.1", 8080))? .run() .await } Of course, anything with brackets can be improved ! ;-) from sanic import Sanic from sanic.request import Request from sanic.response import text, HTTPResponse app = Sanic("MyHelloWorldApp") @app.get("/hello/<name>") async def hello_world(request: Request, name: str) -> HTTPResponse: return text(f"Hello, {name}")
- miiiiiike 4y agoI can’t get over how clunky Python 3 is getting. Does anyone like working with type hinting in Python? All of my code is typed but compared to basically every other type system the process was far more painful than it should have been. Even years later I still keep printouts of the typing documentation next to me so I can save time when I invariably need to look up the multiple ways you can define ‘T’ when it would just be ‘class Foo<T>’ in almost any other language. I never have this problem C++/C#/TypeScript. I’d love to see type hinted Python look more like TypeScript. I use Python every day and I like it less and less as it gets more of the features I want/need to use. I want types, I want generics, I want abstract classes, I want enums, I want interfaces. But I don’t want to have to import all of those features from modules when they should be built into the language itself.. When they’re built into the syntax of practically every other language that supports them. When was the last time you saw a `Protocol` in the wild? Just add interfaces. Teaching Python to a new developer is a ridiculous experience. There are just too many, usually half-baked, ways of doing things. Even this article, which does a great job going over some of Python’s newer/more advanced features, doesn’t use the latest version of the syntax or the generic versions of most types. If I didn’t have such a massive investment in Python I would have moved to Rust by now. And don’t me wrong, I love Python, I want to use it. I just wish it would stop eating its tail. Let’s just do a Python 4. Even if it means another decade long 2/3 adoption foot dragging.
- movedx 4y agoPersonally, I switched to Go.
- Sosh101 4y agoI've been thinking about getting into go - do you mind giving some pros/cons?
- sidlls 4y agoSome pros: * Go is "simple" (the language has a "small surface area", if you will) * Go has a broad and deep standard library that is generally well-documented * Go comes with a reasonably good general-purpose build tool (it builds, formats code for you, runs tests, etc.) Some cons: * Go's structural subtyping is a pretty terrible approach to handling the problem it's meant to solve. It's not quite as bad as a runtime failure when a 'type' doesn't implement a method (since that would be caught at compile time in Go), but it leads to what I consider an abusive over-reliance on ad-hoc interfaces that just get in the way of understanding the code (runtime or test code) * Go's error handling method is inefficient and generally awful. I'm not talking about the verbosity (it's a relatively small issue, in my opinion), I'm talking about that in Go errors are just strings with a gross, inefficient library and system for adding context * Go's generics implementation is, to put it mildly, inadequate; doing anything mildly interesting with it will be a cumbersome chore * Go's handling of dependencies...leaves something to be desired
- FredrikMeyer 4y agoOne of the less talked about effects of type hinting in Python is that editor integration gets a lot better. Better autocomplete is a great help.
- kobzol 4y agoYep. This is actually one of the most useful features for me, autocompletion and Go to definition. I don't actually even use an actual type checker in most projects, unless they are more production-critical.
- synergy20 4y agorust is great indeed, meanwhile it's hard to learn. hard to learn language typically can not become very popular(measured by market share) because its framework, tools, libraries,etc will be limited no matter how great the language itself is, and that's a big problem. It will compete with c++ and c for system programming, it's a hard but it is making inroads gradually, it will take a few decades to replace some core pieces of the fundamental code base(linux kernel, openssl,etc). out of the system programming field, we have Javascript for the web, Go for the cloud, python for AI and ML, C++ for gaming, C for embedded, etc. It will be very challenge for Rust to do well in all these fields. unless it's safe AND beginner friendly(easy to use and safe to use, which it is not) that is. I spent sometime to seriously learn Rust, eventually came back to modern c++. The good thing is that all modern languages are learning from each other, so, in the long term, maybe language itself won't matter that much. Still, it must have both, easy to use and safe to use, for me, python has the former, rust has the latter, none has both.
- fermigier 4y agoPython is still my favourite language, 25+ years after starting using it professionally. Yes, it has become vastly more complex, but its core principles of readability, simplicity, and explicitness continue to make it a versatile and powerful language for both beginners and seasoned developers. And I personally welcome "recent" additions to the language (gradual typing, pattern-matching, asyncio) as they reflect Python's (and its community's) ability to adapt and evolve, remaining relevant in a rapidly changing technological landscape.
- the__alchemist 4y agoI do something similar. You can probably do a similar style in most imperative languages. The key features that make this work in Python are Dataclasses, enums, and type hints. Pydantic if you're looking for a Serde analog. Struct and enum-oriented programming.
- tored 4y agoThis is how I write PHP. The author made a trip to rust to come to this conclusion, what I did was to start using PhpStorm many years ago, that helped me remove many bad habits, like relying on magic and stringly types.
- cannabis_sam 4y agoThis reminds me of learning Haskell… Having transitioned from Java to Python, because the type system in Java had very little bang for the buck, I was under the wrong impression that types sucked and dynamically typed languages were superior. Boy was I wrong about that. I of course don’t try to import all the structural or semantic haskellisms into other languages, but just recognizing the immense value you get from typing your inputs and outputs has changed the way I program.
- hummingn3rd 4y agoMost of the issues described there are solved for me by using pydantic: https://docs.pydantic.dev/latest/ https://docs.pydantic.dev/latest/ (whose core has actually been recently rewritten in rust ^^). To avoid switching parameters with the same type, I like to use * in my methods definitions instead of NewType to force naming parameters. Pydantic allows to validate methods parameters too (see https://docs.pydantic.dev/latest/usage/validation_decorator/ https://docs.pydantic.dev/latest/usage/validation_decorator/) but this is at runtime and can add a performance overhead so it could be enough for the modules interfaces with the rest of the code only. For static checking, the NewType method is probably better, using a simple * avoided us many mistakes already.
- okasaki 4y ago> I have no idea what is going on from the signature itself. Is records a list, a dict or a database connection? Is check a boolean, or a function? What does this function return? What happens if it fails, does it raise an exception, or return None? [...] This type of objection is only raised by people who have never seriously used Python before. It sounds convincing that this is a serious flaw in Python, but in practice it never comes up because when you know what domain you're working in it's always immediately obvious what types the arguments are.
- Shish2k 4y ago> This type of objection is only raised by people who have never seriously used Python before How serious is “serious”? Because I’ve worked on millions-of-lines python projects powering billion-dollar companies, and I 100% agree that maintaining untyped python code that somebody else wrote (or that I wrote myself, 6 months ago) is a nightmare for exactly these reasons...
- okasaki 4y agoI mean I can't even imagine what would lead anyone to write millions of lines of Python for a single project, so maybe there's a level of serious above what I do. Maybe you're doing it wrong?
- esafak 4y agoHow many engineers does your company have? When you have thousands it is easy to write millions of lines of code.
- dgan 4y ago> it never comes up because when you know what domain you're working in it's always immediately obvious what types the arguments are. Oh this one is hilariously wrong. Is it InvoiceLine that we are getting here? Or Maybe LineInvoice? Nope those aren't the same. But the only argument is called «line». How shameful. Much sadness. Better add a print statement, push a new build and comeback in twenty minutes
- adsharma 4y agoIf you write python sufficiently like rust, you should be able to transpile it into actual rust. I spent a few months last year writing py2many. But was surprised by how ChatGPT outperformed it. I do however believe that there is room for a good transpiler and a language ecosystem for a dialect of python you're proposing here. It needs a community and a lot more work.
- CoastalCoder 4y agoI'm curious how much our early programming experiences fix (as in, make permanent) our mindset about types. As a little kid, I dabbled a bit in BASIC. But my more formative years, in high school and college, really centered on statically typed languages: Pascal, then C++. In the 20+ years since then, statically typed languages have always seemed far saner to me than, e.g. Python. I can think of various explanations for this: (a) Because I got my start in statically typed languages, that became ingrained as my "natural" way to reason about programs. Which was self-reinforcing, as practice reinforced my ability to express program constraints using types. (b) I'm naturally biased toward writing correct programs, rather than rapid prototyping. So I would have gravitated towards statically typed (or even proven) programs regardless of my early education.
- esafak 4y agoI had personal experience with statically-typed languages before I ventured into industry with Python. Years of working in large python code bases revealed the wisdom of once again returning to statically-typed languages.
- kobzol 4y agoThis probably plays a role. But for me, I started with C#, but still after I later used Python, I wasn't using types at all. Only after I got more experience in programming, and especially was exposed to Rust, then I started to write Python in a.. different way :)
- toxik 4y agoI feel like the elephant in the room is that for most cases in this blog post, you either use NumPy arrays or something else like it. You can make ndarray subclasses and that helps but type hinting and NumPy feels so… poorly done.
- patrickkidger 4y agoTry using jaxtyping: https://github.com/google/jaxtyping https://github.com/google/jaxtyping. It also supports numpy/pytorch/etc.
- gigatexal 4y agoI like this. I’m a huge fan of types. Even in my silly small programs I find them self documenting of the code I write and it helps me when I come back days later and wonder wtf was I thinking or what am I doing etc.
- crabbone 4y ago[flagged]
- fnordpiglet 4y agoI stopped writing anything like rust because rust its ecosystem is mostly complete enough , expressive, and rapid to prototype and use. To save my cognitive load I shelved my python, Bourne, JavaScript, etc, and just write rust now. It’s not a decision for everyone and I’m not here to convince anyone of anything. But my life is simpler, my scripts more general and faster, and I don’t particularly ever miss the interpreted languages. It took a while to build that level of facility in rust for simple tasks, and of getting over the mental wall of “python is for X and compiled is for Y.” But I’m generally glad I did. Except when it comes to coding interviews and none of the graders know rust and my Python is getting rusty (harhar) after a few years of leaving it aside.
- unethical_ban 4y agoIn my profession (sysadmin related) it seems that if you want the glue-code orchestration and SDK availability of Python, but with better performance and types, you use golang. I don't think most vendors are popping out Rust SDKs for the product's APIs. I'm not invalidating your experience, it sounds wonderful to find the solace in one language.
- fnordpiglet 4y agoYeah just similar to Python the build and package management stuff in go is a nightmare, and I’d rather hitch my cart to the Mozilla guys than the Google guys. (I’m ex-Netscape and helped launch the OSS of the browser into Mozilla) On apis, honestly, I just write my own api integrations. But most of the APIs I use are documented without programmatic specifications, and sdks are usually garbage written by an intern :-)
- dehrmann 4y agoI was expecting weird magic with memory safety, but it was "use types."
- kobzol 4y agoSorry :) Not that kind of "like Rust".
- __MatrixMan__ 4y agoI like these ideas, and practice many of them regularly. Unfortunately one gets the feeling that they're swimming upstream a bit of the time. For instance: data classes instead of tuples or dicts. Great, love it. Now add immutability (frozen=true isn't enough so you're probably going to need pyrsistent) and serialization (dataclasses_json works reasonably well for this), and precommit hooks with pyright or mypy... And you've got yourself a reasonable system for leaning on the type checker. Except you've been at it all day and you still haven't gotten any "work" done, and you've got all of these weird constructions that aren't complex themselves but they'll make a newcomer to your project go "huh?" Sometimes it's a necessary evil. My users write python, so I need to write python to ensure that their experience is nice and ergonomic. But if I try to write python like it's rust (which I usually do) I end up with quite a pile of things that add toolchain complexity without addressing any business problems. Don't get me wrong, I still do it, but I've never managed to make it look like a good enough idea to get other people to join me, which is maybe the universe trying to tell me I should stop.
- kobzol 4y agoI agree that it's not easy to explain this to other people, and Python tooling in general is pretty terrible. OTOH, having types in the code and giving newcomers to the codebase the ability to actually see what types "flow" through functions, and having the ability to "Go to definition" is a big benefit for introducing old Python code to newcomers.
- adultSwim 4y agoOne thing I liked about the post was I can sell some of the suggestions to the non-engineer Python users I work with. Types in signatures and data classes are big wins overall. While I miss working with OCaml, Python's pay-as-you-go style of typing meets many users where they're at.
- k1next 4y agoI really like type hints in Python. It allowed me to catch several errors before running code. But it isn't always the most straight forward process. However, my most frequently occurring thought is: I'm doing all the typing, I wish I could get some performance benefits out of it.
- kobzol 4y agoGood point! Guido mentioned that maybe in the future, the new "mini-JIT" in CPython might be able to leverage the type hints. I also want to have this as a thesis topic for my students, to try it out.
- davidatbu 4y agoThat would be interesting! You might already be aware. But there's mypyc[0], which is an AOT compiler for Python code with type hints (that, IIRC, mypy uses to compile itself into a native extension). Wanted to give you a head-start on the lit-review for your students I guess :) [0] https://github.com/mypyc/mypyc https://github.com/mypyc/mypyc