Xolve Xolve - 6 months ago 45
Python Question

Type checking of arguments Python

Sometimes checking of arguments in Python is necessary. e.g. I have a function which accepts either the address of other node in the network as the raw string address or class Node which encapsulates the other node's information.

I use type(0 function as in:

if type(n) == type(Node):
do this
elif type(n) == type(str)
do this

Is this a good way to do this?



No, typechecking arguments in Python is not necessary. It is never necessary.

If your code accepts addresses as rawstring or as a Node object, your design is broken.

That comes from the fact that if you don't know already the type of an object in your own program, then you're doing something wrong already.

Typechecking hurts code reuse and reduces performance. Having a function that performs different things depending on the type of the object passed is bug-prone and has a behavior harder to understand and maintain.

You have following saner options:

  1. Make a Node object constructor that accepts rawstrings, or a function that converts strings in Node objects. Make your function assume the argument passed is a Node object. That way, if you need to pass a string to the function, you just do:


    That's your best option, it is clean, easy to understand and maintain. Anyone reading the code immediatelly understands what is happening, and you don't have to typecheck.

  2. Make two functions, one that accepts Node objects and one that accepts rawstrings. You can make one call the other internally, in the most convenient way (myfunction_str can create a Node object and call myfunction_node, or the other way around).

  3. Make Node objects have a __str__ method and inside your function, call str() on the received argument. That way you always get a string by coercion.

In any case, don't typecheck. It is completely unnecessary and has only downsides. Refactor your code instead in a way you don't need to typecheck. You only get benefits in doing so, both in short and long run.