Skip to content Skip to sidebar Skip to footer

How To Specify Argument Type In A Dynamically Typed Language, I.e. Python?

Is there any such equivalent of Java String myMethod (MyClass argument) {...} in Python? Thank you, Tomas

Solution 1:

No. (And more stuff to round this up to 15 characters...)

Solution 2:

No, there is not.

In fact, checking types is considered "un-Pythonic", because an object of any type that looks enough like the expected type should be treated equally.

Solution 3:

Python 3.x has function annotations where you can declare argument and return types:

defmyMethod(argument: MyClass) -> str:
   ...

But currently Python does nothing with them, they serve as documentation only.

Solution 4:

I just want to say that I'm in full agreement that type checking is evil. But python is also incredibly flexible and I'm in the mood to be evil. This code will take effect at runtime and not compile time. You could do something similar for return type. Something like this could be useful for debugging and, because it's a decorator, it's easy enough to remove.

For it to be useful for debugging you would have to have a situation where two types had all the same attributes that were getting accessed but with different semantics. So that's a pretty limited case. Other than that, you're about to get a typerror anyways when this code runs. The good news is that this is almost never a problem. I really don't know why people from statically typed languages make such a big deal over it.

deftypes(*args, **kwargs):
    arg_types = args
    kwarg_types = kwargs
    defdecorator(f):
        deffunc(*args, **kwargs):
            for arg, arg_type inzip(args, arg_types):
                ifnotisinstance(arg, arg_type):
                    raise TypeError("Wrong type suckah")
            for kw, arg in kwargs.items():
                ifnotisinstance(arg, kwarg_types[kw]):
                    raise TypeError("this is a bad error message")
            return f(*args, **kwargs)
        return func
    return decorator

@types(int, str, bool, flag=bool)defdemo(i, strng, flag=False):
    print i, strng, flag

demo(1, "foo", True)

try:
    demo("foo", "bar", flag="foobar")
except TypeError:
    print"busted on posargs"try:
    demo(1, "foo", flag=2)
except TypeError:
    print"busted on keyargs"try:
    demo(1, "foo", 3)
except TypeError:
    print"no use sneaking it through"

Solution 5:

No.

In Python, it's the program's responsibility to use built-in functions like isinstance() and issubclass() to test variable types and correct usage. Python tries to stay out of your way while giving you all you need to implement strong type checking.

from Why is Python a dynamic language and also a strongly typed language. Also

In a dynamically typed language, a variable is simply a value bound to a name; the value has a type -- like "integer" or "string" or "list" -- but the variable itself doesn't. You could have a variable which, right now, holds a number, and later assign a string to it if you need it to change.

Further, isinstance() and issubclass() can be used to do type-checking. If you want to make sure that argument is of MyClass type, you can have a check inside the function. You can even type-cast the value of the argument (if you have a constructor accepting such value) and assign it to my_object.

Post a Comment for "How To Specify Argument Type In A Dynamically Typed Language, I.e. Python?"