Question

A dynamically typed language (i.e Python) performs Type Checking at run-time, so a variable or parameter can refer to a value of any type. Does the language definition requires the variables and parameters not be typed? Would it make sense to modify the language to have variables and parameters with types?

Was it helpful?

Solution

This question is probably gonna get closed unless gracefully saved by someone like @jon-Skeet.

Till then, I believe anyone who is coming from a strongly typed programming background will argue for at least the option to specify typing. But at the end of the day, the creators/modifiers of the language disliked the requirement for typing enough to not put any effort to allow for the possibility of specifying type. So I wouldn't count on it happening even if it may make real good sense.

From my brief research, it appears python does not allow to specify types. But you can check the type using isinstance http://docs.python.org/library/functions.html#isinstance

Now as a matter of personal opinion, I say why not. If the maintainers/ creators of the language in question can do it in a way that doesnt compromise efficiency/ the actual intent of the langauge, they should at least allow someone who is interested to add the capability for specifying type to do so.

OTHER TIPS

You can mix static and dynamic typing; there are plenty of languages that do that.

In the simplest case, you'd have a class hierarchy comprising all types rooted at object (using Python terminology). Every "untyped" variable would have type object so it can hold any variable. Other variables could have more specific types, such as int.

An implementation would still need to perform run-time type checking, but it could optimize away the checks where the types can be determined at compile time.

Cython does something similar, except that it offloads the static typing part to a C compiler:

def foo():
    cdef int i = 0  # statically typed variable
    ham = "spam"    # dynamically typed variable

    while i < 10:
        print(ham)
        i += 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top