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

25

u/13steinj Jul 16 '18

In a “Strongly-Typed” language, you have to specify the type of a variable when it is declared. Those would include C, C++, Java, C#, Go.

In a dynamically-typed language, there are still the concept of types, but the type of a variable is dynamic.

Yet another person doesn't know the difference between strong typing and static typing, apparently.

0

u/[deleted] Jul 16 '18

[deleted]

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:

>>> print(4 + 5)
9

Here, 4 and 5 are both instances of type int. 4 + 5 evaluates to 9 which is also of type int, and is equivalent to int(9). Each int object has a __str__() method. After evaluating the 4 + 5 expression, the __str__() method from the resulting object int(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 calling int(9).__str__() in order to get a string that means the same thing to humans as 9.

Likewise:

>>> print('dog' + 'house')`
'doghouse'

Here, 'dog' and 'house' are both of type str, 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 as str('doghouse')) is called in order to get a string representation of that object,

However, consider the following:

>>> print(9 + 'doghouses')
Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: unsupported operand type(s) for +: 'int' and 'str'

You might have expected print(9 + 'doghouses'), to print 9doghouses, but instead, you get this TypeError. This is Python's way of telling you that while the + operator works for both types str and int, this operator can't be used between the two types. While 9 is the same as int(9), and we know that int(9).__str__() will return a string version of 9, '9', Python will not make that conversion from type int to type str implicitly. You have to explicitly tell it that you want a string representation of 9 by calling the built-in function str(9), which is indirectly calling int(9).__str__().

>>> print(str(9) + 'doghouses')
9doghouses

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.

3

u/[deleted] Jul 16 '18

Thanks for the explanation!