While studying C# I found it really strange, that dynamically typed Python will rise an error in the following code:
i = 5
print i + " "
whereas statically typed C# will normally proceed the similar code:
int i = 5;
Console.Write(i + " ");
I would expect other way around (in python I would be able to do this without any casting, but C# would require me to cast int to string or string to int).
Just to highlight, I am not asking what language is better, I am curious what was the reason behind implementing the language this way.
A statically-typed language is a language (such as Java, C, or C++) where variable types are known at compile time. In most of these languages, types must be expressly indicated by the programmer; in other cases (such as OCaml), type inference allows the programmer to not indicate their variable types.
Statically Typed Languages Some common examples of programming languages that belong to this category are Java, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift.
Julia is squarely in the dynamic camp: types are a property of values not expressions. The result type of code is determined by how values flow through it when it executes; the language does not include any rules for assigning types to expressions before executing them.
Statically typed languages have better performance at run-time intrinsically due to not needing to check types dynamically while executing (it checks before running). Similarly, compiled languages are faster at run time as the code has already been translated instead of needing to "interpret"/translate it on the fly.
You are right that something is odd here. What is odd is that +
means string concatenation in either language! Whether +
can concatenate an int onto a string or not is a small point compared with the oddity that is +
meaning "concatenate" at all.
I do not know which language first allowed +
to be used to mean string concatenation, but that misfeature has been repeated over and over again so often that now we typically don't even notice how truly strange it is. Let's list some of the properties of normal addition and see if strings measure up.
One out of four ain't good. String concatenation is very unlike addition, so why should they share an operator?
I don't say this often but C got it right. The string concatenation operator should be just that: concatenation. In C "X" "Y"
means "XY"
. If you put two string literals beside each other, they concatenate into a third.
This isn't really a static/dynamic question but rather a matter of language design philosophy.
In Python, +
is defined to be addition for numbers and concatenation for strings. This is perfectly reasonable behavior if you have even a little programming experience.
But when you have one of each, what happens? Does it try to convert the string to a number, or does it convert the number to a string? Either is, again, perfectly reasonable behavior to anyone who has done any programming at all, but since different languages have different rules about how this happens, you may make assumptions different from the ones someone else would make depending on which languages you have experience with already.
Python has as one of its guiding principles "explicit is better than implicit" (import this
) and so it makes you explicitly state which behavior you want. How? By converting either the string or the number to the desired type, of course. Then they are both strings or both numbers and the behavior is obvious.
The resulting code is easier to read (even if you don't know much Python), because you don't have to guess what it's going to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With