Skip to content Skip to sidebar Skip to footer

How Can I Avoid Type Checking A Python Object If Its Attributes Aren't Used?

I've come across answers here for type checking in general, type checking for numbers, and type checking for strings. Most people seem to respond by saying that type checking shou

Solution 1:

A common duck-typish Python solution to this problem is to (try to) convert what you got to what you need. For example, if you need an integer:

defgetconstants(arg):
    try:
        arg = int(arg)
    except:
        raise TypeError("expected integer, or something that can be converted to one, but got " + repr(arg))

The int type constructor knows how to deal with many built-in types. Additionally, types that are convertible to an int type can implement the __int__() special method, which would allow them to be used here. If the user passed in a string, that would work fine too as long as it contained only digits.

Your idea of performing some operation that could only be performed on a numeric type (such as adding 2) is similar, and would work great in many cases, but would fail with strings that can be converted to the desired type, so I like the type conversion better.

You could probably get by without the try/except here, since int() will raise either TypeError or ValueError if it can't do the conversion, but I think TypeError is more appropriate since you are mainly interested in the object's type, so I chose to catch the exception and always raise TypeError.

Solution 2:

My honest answer to

Most people seem to respond by saying that type checking should never be performed in python (< 2.6) due to duck typing

is: nonsense.

Type-checking - were needed - is common practice.

Don't listen to all and everything and don't accept every statement unfiltered.

Solution 3:

You can check to make sure you're expecting a type you support initially. i.e.

deffoo(arg):
    ifnotisinstance(arg, int):
        raise TypeError
    ...

Nothing wrong with that if you're only supporting integers.

Solution 4:

Introspection works for this...primitive types have class names too:

>>>i=2>>>i.__class__.__name__
'int'
>>>s="two">>>s.__class__.__name__
'str'
>>>

Post a Comment for "How Can I Avoid Type Checking A Python Object If Its Attributes Aren't Used?"