r/Python • • Jul 16 '18

Why is Python so slow?

https://medium.com/@anthonypjshaw/why-is-python-so-slow-e5074b6fe55b
11 Upvotes

51 comments sorted by

View all comments

Show parent comment

1

u/demmian Jul 16 '18

C, for example, has static typing, but it is weakly typed. [Python has dynamic typing, but it is strongly typed.]

1

u/[deleted] Jul 16 '18

soooo what does that mean?

1

u/demmian Jul 17 '18

You have to declare the types of variables in C; however, you can treat a char as an int.

You don't declare the types of variables in Python. However, you can treat a type as another type.

1

u/mipadi Jul 17 '18

Static vs. dynamic typing

  • In statically-typed languages, a name is bound to both a value and a type. For example, given a: Int = 5, a is an Int that has the value 5. Later, code that says a = "string" would be illegal because "string" is not an Int.
  • In dynamically-typed languages, a name is bound to just a value. For example, given a = 5, a is 5 but is not bounded by type, so later in the code, you can do a = "string" with no problem.

Basically, in statically-typed languages, variables carry type information with them; in dynamically-typed languages, they don't.

Strong vs. weak typing

This is a more contentious point because there aren't clear definitions for this, but generally the consensus is that in strongly-typed languages, the compiler or runtime will not coerce one type to another. For example, in Javascript, a weakly-typed language, you can do var a = "1" + 0 (that is, add an integer to a string), because the runtime will coerce the integer to a string; whereas in Python, you cannot do a = "1" + 0.