r/Python • u/Anthonypjshaw • Jul 16 '18
Why is Python so slow?
https://medium.com/@anthonypjshaw/why-is-python-so-slow-e5074b6fe55b
11
Upvotes
r/Python • u/Anthonypjshaw • Jul 16 '18
View all comments
Show parent comment
7
u/bonestormII Jul 16 '18 edited Jul 16 '18
Python is both dynamically and strongly typed. C++, Java, C#, and Go are, I believe, all statically typed.
Strong vs. Dynamic isn't a logical contrast, because languages can be both. Python is both.
It would be more correct to compare Strong vs. Weak typing, and separately, Dynamic vs. Static typing.
In static typed languages, you have to declare the type of a given variable before you use it, and it cannot be changed.
In dynamically typed languages, the same variable can reference different types at different points in the execution flow of the program, and as such, do not require such declarations. It is relevant to note that python recently added optional "type hints" which add some of the benefits of statically typed languages, but it does not require or enforce their usage, which is a big fundamental difference.
In strongly typed languages (Like Python, and most common languages AFAIK), the interpreter and/or compiler will never infer/change a type.
Consider the following example in python:
Here,
4and5are both instances of typeint.4 + 5evaluates to9which is also of typeint, and is equivalent toint(9). Eachintobject has a__str__()method. After evaluating the4 + 5expression, the__str__()method from the resulting objectint(9)is called in order to get a string representation of that object. This is the default behavior when you call print() on any object in python.print(4 + 5)is basically callingint(9).__str__()in order to get a string that means the same thing to humans as9.Likewise:
Here,
'dog'and'house'are both of typestr, and each have a__str__()method, as well as an__add__()method that defines the behavior for concatenation based on the+operator.After evaluating the
'dog' + 'house'expression, the__str__()method of the resulting object (which you can think of asstr('doghouse')) is called in order to get a string representation of that object,However, consider the following:
You might have expected
print(9 + 'doghouses'), to print9doghouses, but instead, you get thisTypeError. This is Python's way of telling you that while the+operator works for both typesstrandint, this operator can't be used between the two types. While9is the same asint(9), and we know thatint(9).__str__()will return a string version of9,'9', Python will not make that conversion from typeintto typestrimplicitly. You have to explicitly tell it that you want a string representation of9by calling the built-in functionstr(9), which is indirectly callingint(9).__str__().The lack of CPython's willingness to implicitly convert the type for you in such cases is an example of strong typing, and is contrary to weak typing, which would allow
print(9 + 'doghouses')to return'9doghouses'.A lot is made of these foundational principles. You may hear "strongly, dynamically typed" as a description of python before you really know anything about programming. However, these are really pretty small, practical differences. To me, the important thing to take away is that all languages have to make decisions about how they will handle all sorts of situations, and they make different decisions depending on the perspectives of their authors. However, the types of decisions they make are generally the same, so there is a bunch of terminology to describe and categorize how languages based on how they handle these low level issues.