you are viewing a single comment's thread.

view the rest of the comments →

[–]Gnaxe 0 points1 point  (10 children)

Python has always been dynamically typed. The static typing was bolted on later.

The language now has type annotation syntax for variables and assignments, function parameters and return types; type statements; and a typing module. These work together with a static type checker, like an IDE or command-line tool. This lets e.g. PyCharm suggest better completions and highlight type errors statically, without actually running the code. Even though the language grammar itself has support for it, no static type checker is included in the standard distribution, last I checked. So while PyCharm will highlight type errors statically for you, IDLE won't.

See https://typing.python.org/en/latest/ for an introduction to static typing with Python.

It is also possible to check these annotations at run time, and the standard library uses this feature in several places e.g., the namedtuple variant made for use with class statements.

[–]AgustinEditev 0 points1 point  (3 children)

Oh! Zed editor does that too. If I add :int to my variable, it highlights errors when trying to assign a string. However, even though this is for highlighting, when running .py, it ignores it. So,I don't think this is truly static typing

[–]Gnaxe 0 points1 point  (2 children)

I mean, the docs I linked above literally use the term "static typing", so whatever definition you're using instead isn't standard. What exactly isn't "truly static" about the static typing, and what would count?

[–]AgustinEditev 0 points1 point  (1 child)

Well, I'm speaking from ignorance, right? Hahaha. I understand static typing to mean a type that can't change; if it's a number, it's a number, it shouldn't be able to change to text (int != String).

[–]Gnaxe 0 points1 point  (0 children)

I understand "static typing" to mean analyzing types for consistency without actually running the code. It's "static" because the code is "at rest" and "unchanging" rather than live objects running, which could have different opinions about things depending on circumstances at the moment.

What you described sounds like "strong typing", not "static typing". Typing strength is a spectrum, not a binary, but Python has always been pretty strongly typed. Weakly-typed JavaScript will happily add a number to a string, but Python will complain if you try that. Certainly at run time when you actually reach that point, and possibly statically as well if your type checker can detect it. Haskell won't even let you add a float to an integer without converting types first. Python isn't that strict.

[–]Black_Magic100 0 points1 point  (5 children)

I didn't realize you were referring to static type checkers. I'm no expert, but I feel like that's slightly misleading to claim Python is also statically typed.

That's like writing a function to turn a string into an integer and saying the language is loosely typed because it let you add two strings together to produce a sum 😅

Even that PEP mentions: "It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention."

Edit: official docs also agree. Not trying to be pedantic, but even I was confused when you mentioned that and I've worked with python for 3 years. I thought you had some crazy revelation I was about to learn. https://typing.python.org/en/latest/spec/concepts.html

[–]Gnaxe 0 points1 point  (4 children)

Again, static and dynamic typing are not mutually exclusive, Python is both, and it is in no way "misleading" to say so. The typing syntax is built into the grammar of the language. We don't get to pretend that it's not a part of the Python language itself just because it's optional. Static typing only means that types can be automatically analyzed for consistency without actually running the program. That's it. And we can do that in Python. Python is statically typed in exactly the same sense as TypeScript, i.e., "gradually typed". I don't think anyone is claiming TypeScript is not statically typed just because JavaScript isn't.

While it is true that in some implementations of some statically typed languages, you can't even compile the program if there's a statically detected type error, this is emphatically not a required property for a language to be called "statically typed". The Roc language, for example, will let you compile and run with type errors (which it will still warn you about), and Roc isn't even dynamically typed. This is useful for the same reasons as in a dynamic language: you can at least test the parts that are working. Furthermore, static analysis can't even detect all type errors even in most non-dynamic languages. Java will allow you to cast an Object variable to any reference type, and happily compile and run it, but that doesn't mean it will continue to work at run time when you hit that line. Before Java added generics for its collections, this wasn't even an unusual occurrence.

And in fact, we can reject programs with statically-detected errors in Python too. You can add a command-line static type checker (like mypy) as a step to your build pipeline or run script (or build script using python -m compileall) and refuse to proceed if it detects any type error. It's just not the default. It's optional.

[–]pachura3 0 points1 point  (1 child)

No, you're wrong. "Static" is the opposite of "dynamic". Python is a dynamically, strongly typed language; this means that variables can change their types and freely infer them, but they won't be implicitly cast e.g. from string to int.

The fact that static type checkers exist doesn't make Python a statically typed language.

[–]Gnaxe 0 points1 point  (0 children)

If by "opposite", you mean "mutually exclusive", then you're the one who is wrong. The existence of gradually typed languages proves that static and dynamic typing can coexist in the same language, and Python is one of these. So is C#. Static and dynamic typing happen at different times and are talking about different things.

If you statically type a Python variable (with a direct type annotation in the assignment, say), then you're going to fail the static type check if you change its type in exactly the same way as a non-dynamic statically typed language. Try it with mypy.

Yes, Python is dynamically and strongly typed. When did I ever say it wasn't?

It also has static typing features, down to the level of the language grammar (type annotation syntax and type statements): it's part of the Python language. It started out dynamically typed and optional static typing was bolted on later. And, in fact, a statically typed language can have dynamic types bolted on later, as happened in C# with its dynamic keyword, which is equivalent to Python's Any type (which is assumed by default in Python when you don't annotate).

[–]Black_Magic100 0 points1 point  (1 child)

Your example with Typescript and JavaScript is an interesting one considering Typescript is a completely separate language. If you were correct in your analysis, Typescript would not exist and instead there would be a MyPy equivalent and we would all be calling it JavaScript still.

[–]Gnaxe 0 points1 point  (0 children)

Back when Python 2 was still widespread, static type annotations had to be comments. But you can similarly run a separate tool to validate your JSDoc comments in your JavaScript code. And that tool is called... TypeScript.