-5

How come we can make a variable point to a different type of value during the lifetime of a program in python but not in swift? this works in python:

x = 0
x = "zero"

swift throws error:

var x = 0;
x = "zero";

is this behaviour associated with strong/weak typing concept or static/dynamic concept?

aziz
  • 81
  • 1
  • 7
  • You don't need `;` in python – user3483203 Mar 12 '18 at 17:44
  • Strict data type concept – AamirR Mar 12 '18 at 17:46
  • https://stackoverflow.com/questions/2690544/what-is-the-difference-between-a-strongly-typed-language-and-a-statically-typed – Code Different Mar 12 '18 at 17:46
  • `;`'s neither in `Swift` – AamirR Mar 12 '18 at 17:47
  • sorry accidently added semicolon(;) in python code – aziz Mar 12 '18 at 17:49
  • why are you fixated on the ;? my question is simple and clear the semicolons dont change my main question – aziz Mar 12 '18 at 17:50
  • 3
    Swift is strongly typed. `x` is an `Int`. You can't assign a `String` to an `Int` variable. – rmaddy Mar 12 '18 at 17:51
  • 1
    Read about [Type Safety and Type Inference](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID322) in the Swift book. – rmaddy Mar 12 '18 at 17:53
  • 1
    Swift is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code requires a String, you can’t pass it an Int by mistake. - from Type Safety and Type Inference – Hyeonju Kim Mar 12 '18 at 18:00
  • Do you really care about what the best labels are to apply to the two languages' type systems, or about the actual differences between the two languages (and other languages)? Because the latter seems a lot more important to me than arguing about whether Swift counts as "statically typed" according to some specific definition, or whether that definition is a good one, or… – abarnert Mar 12 '18 at 18:46

1 Answers1

2

Both Python and Swift are strongly typed as compared to languages like C and Lisp. Every type a value can have is expressed in the language, and there's no way to interpret a value as a different type without doing something explicitly marked "unsafe".

Both Python and Swift are weakly typed as compared to languages like Haskell, Rust, and most ML variants. There are many things that could be conceivably usefully expressed in the type system but aren't, most obviously mutation of I/O streams—a file that you've written to is the same file as before you wrote to it.


As for static vs. dynamic, those are badly overloaded terms. You can argue about which definition of "dynamic typing" is the best definition, but that doesn't get you anywhere; what matters the differences between the languages, which exist no matter how you label them.

In both languages, values are (strongly) typed. But in Python, variables are untyped names; in Swift, variables are typed locations that hold values.

In Python, it's not just possible, but idiomatic, to do ad-hoc polymorphism at runtime, aka "duck typing"; that's not true in Swift.

Both languages have type annotations and rules for type inference. In Swift, the compiler applies these rules and rejects your code if it can't validate a type for everything. In Python, the compiler ignores annotations and infers nothing, and you have to use an optional tool like Mypy if you want to type-check your code.

Python and Swift both support at least some form of OO with dynamic dispatch. That is, even in Swift, a Base variable may hold a Derived value (ignoring the issues about value-vs.-reference semantics here). However, Python does it Smalltalk-style, effectively looking it up via reflection, while Swift does it C++-style, using a statically-compiled table to look up method calls at runtime. Except that Swift has an escape route for Objective C objects, which handle dispatch Smalltalk-style.


Partly as a consequence of some of the above differences, Swift's runtime type system is exactly as strong as its static type system, but Python's is stronger. There are type distinctions that can exist at runtime in Python that can't be expressed in the static typing language. Whether this means Python is more expressive than Swift or Swift is safer than Python is a fun question for debate. (Of course many of those distinctions are expressible in the type system of, say, Haskell, so you can say that Haskell is more expression than Swift and safer than Python.)

Another important difference that isn't really about strong vs. weak or static vs. dynamic is that Swift encourages some level of type-driven programming—e.g., it's idiomatic to do things like pattern-match on algebraic types. In Python, on the other hand, type-switching of all kinds is discouraged. It's not that you can't do it (although it doesn't have nice syntactic sugar like Swift), but that it's almost always considered more Pythonic to do something else (whether OO, @singledispatch, etc.).

abarnert
  • 334,953
  • 41
  • 559
  • 636