Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't python infer types like scala? [duplicate]

Possible Duplicate:
How to deal with Python ~ static typing?

I'm basically a Java programmer with little knowledge of python.I really like the syntax of python and the ease with which a programmer is able to express his idea's but also I'm aware that python is dynamically typed and thus is not as fast as Java.My question is why can't python infer type like languages such as scala ?

like image 209
Emil Avatar asked Oct 20 '10 07:10

Emil


1 Answers

It is not that Python can't, but it doesn't. The difference is in the type systems that the designers of the languages choose to follow.

Python uses duck typing and has typed objects but untyped variable names. Type constraints are not checked at compile time; rather, operations on an object may fail, signifying that the given object is not of a suitable type. Despite being dynamically typed, Python is strongly typed, forbidding operations that are not well-defined (for example, adding a number to a string) rather than silently attempting to make sense of them.

Scala is a statically typed language, that is, types are checked at compile time. A local type inference mechanism takes care that the user is not required to annotate the program with redundant type information. Operations that break type constraints leads to compiler errors, not runtime errors. Also see The Purpose of Scala's Type System, especially the section where duck typing is discussed.

like image 94
Vijay Mathew Avatar answered Oct 11 '22 18:10

Vijay Mathew