Nice article. I'm also JavaScript developer, that has to write Python code. One difference that is really surprising (and some time annoying) is lack of type coercion. I assumed that if Python is dynamically typed language, when I passed as an argument numer to method that expected string eg. print, Python did the type coercion internally. However, this is not a case. You have to do it explicitly eg. print(str(1))
Wat? First, why do you call print a method? It is a function. Second, it doesn't "expect" a string, it will take whatever you give to it (its signature starts with *args). And third, of course print(1) works, in every Python I know. What exactly did you try to write and what happened?
Yes, this is the most important distinction. This also frustrates many people coming from languages other than PHP to JavaScript.
Namely, both JavaScript and PHP are not just dynamically typed, but also loosely typed in the sense that values are implicitly casted if it's possible, to match the type expected by function call or operator. Furthermore, in JavaScript there are very odd implicit casting rules that cast operands based on combination of the other operand and operator that aren't always intuitive (the + operator when either operand is a string comes to mind).
Python on the other hand is duck typed, which is a term invented to differentiate it's specific brand of dynamic typing that is not quite strong typing (as it doesn't require explicit interface adherence declaration), but not quite loose typing either (as it doesn't implicitly cast, i.e. it's behavior is more similar to that of, say, Ruby in this regard, than to that of JS or PHP).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice article. I'm also JavaScript developer, that has to write Python code. One difference that is really surprising (and some time annoying) is lack of type coercion. I assumed that if Python is dynamically typed language, when I passed as an argument numer to method that expected string eg. print, Python did the type coercion internally. However, this is not a case. You have to do it explicitly eg. print(str(1))
Wat? First, why do you call
print
a method? It is a function. Second, it doesn't "expect" a string, it will take whatever you give to it (its signature starts with *args). And third, of courseprint(1)
works, in every Python I know. What exactly did you try to write and what happened?Yes, this is the most important distinction. This also frustrates many people coming from languages other than PHP to JavaScript.
Namely, both JavaScript and PHP are not just dynamically typed, but also loosely typed in the sense that values are implicitly casted if it's possible, to match the type expected by function call or operator. Furthermore, in JavaScript there are very odd implicit casting rules that cast operands based on combination of the other operand and operator that aren't always intuitive (the
+
operator when either operand is a string comes to mind).Python on the other hand is duck typed, which is a term invented to differentiate it's specific brand of dynamic typing that is not quite strong typing (as it doesn't require explicit interface adherence declaration), but not quite loose typing either (as it doesn't implicitly cast, i.e. it's behavior is more similar to that of, say, Ruby in this regard, than to that of JS or PHP).