Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid type checking a python object if its attributes aren't used?

Tags:

python

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 should never be performed in python (< 2.6) due to duck typing. My (limited) understanding of duck typing is that type is determined by use of an object's attributes. What do I do if I'm not using any attributes?

I have a simple function that determines constants based on the argument, which should be a number. I raise an exception defined by

class outOfBoundsError(ValueError):
    """
    Specified value is outside the domain.
    """

with a message telling them the number they gave me is too big. I would like to keep this message specific. But if the argument is a string (like 'charlie'), it still considers the argument to be greater than my specified number (and raises my exception). Should I just add a dummy line to the code like argument + 2 so that a TypeError is raised?

Note: I don't know anything about ABCs but I don't think they're available to me since the latest python version we have access to is 2.5 : (.

like image 740
aeroNotAuto Avatar asked Oct 24 '25 15:10

aeroNotAuto


2 Answers

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:

def getconstants(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.

like image 68
kindall Avatar answered Oct 26 '25 05:10

kindall


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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!