Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Is isinstance() necessary in this case?

I defined a class Time that has three int attributes: hrs, min, sec

And I defined methods intToTime() that convert a Time instance to an int, which is the number of seconds in that time, and also a method timeToInt() that do the reverse.

I'd like them to implement __add__, so I can do things like "TimeA + TimeB" or "TimeA + 100", where 100 is the number of seconds add to TimeA.

As I'd like to merge these two (since there's no overloading in Python),

def __add__(self,num):
    return Time.intToTime(self,Time.timeToInt(self)+num)

def __add__(self,other):
    return Time.intToTime(self,Time.timeToInt(self)+Time.timeToInt(other))

"num" is supposed to be an int, "other" is another Time instance. I know one way using isinstance().

But my question is, in this case, how should I implement such an add without using isinstance()?

like image 912
Wu Fan Avatar asked Feb 25 '14 15:02

Wu Fan


People also ask

Should I use Isinstance?

isinstance() returns a boolean - true or false - based on whether the object is of given type. isinstance is usually more elegant to use rather than write a cluttered equality check, for most cases.

Why should I use Isinstance Python?

In Python, you can use the isinstance() function to verify whether a value holds a particular data type. For example, if you want to verify that a list of values is stored as a list, or if a number is stored as a float, you can use isinstance() .

Should I use Isinstance or type?

Conclusions. isinstance is usually the preferred way to compare types. It's not only faster but also considers inheritance, which is often the desired behavior. In Python, you usually want to check if a given object behaves like a string or a list, not necessarily if it's exactly a string.

What are the differences between type () and Isinstance ()?

What Are Differences Between type() and isinstance()? and isinstance() is that type(object) returns the type of an object and isinstance(object, class ) returns True if the object argument is an instance of the class argument or in a direct or indirect subclass relationship.


2 Answers

You really have two choices: EAFP or LYBL. EAFP (easier to ask forgiveness than permission) means use try/except:

def __add__(self, other):
   try:
       return Time.intToTime(self, Time.timeToInt(self)+Time.timeToInt(other))
   except AttributeError as e:
       return Time.intToTime(self, Time.timeToInt(self) + other)

Note that Time.timeToInst(self) is kind of weird; you would normally write that self.timeToInt().

LYBL means look before you leap - i.e. isinstance. You already know that one.

like image 144
Corley Brigman Avatar answered Sep 21 '22 10:09

Corley Brigman


You'd better make intToTime and timeToInt module level functions, the same level as your class Time, and implement your __add__ like this:

def __add__(self, num):
    if isinstance(num, Time):
        num=timeToInt(num)
    elif not isinstance(num, int):
        raise TypeError, 'num should be an integer or Time instance'
    return intToTime(timeToInt(self)+num)
like image 21
zhangxaochen Avatar answered Sep 19 '22 10:09

zhangxaochen