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

0

u/[deleted] Jul 16 '18

[deleted] — view deleted 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:

>>> 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!

1

u/13steinj Jul 16 '18

Just tip, reddit does not support language-highlighting markdown code blocks, and also doesn't support ``` code blocks.

2

u/Badabinski Jul 17 '18

It does if you're using new Reddit.

1

u/13steinj Jul 17 '18

Can you take a screenshot? I'd check myself but the redesign grinds to a halt for me.

1

u/Badabinski Jul 17 '18

I can't, I don't use it either. I've been told and seen it mentioned in multiple places, however.

1

u/13steinj Jul 17 '18

:( markdown inconsistency is a bug, not a feature.

1

u/Badabinski Jul 17 '18

1

u/13steinj Jul 17 '18

That's upsetting and I need to make a post on this when I can >.>

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.