Python Forum
isinstance() always return true for object type check
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
isinstance() always return true for object type check
#1
Hey guys,

I am currently learning class special methods.
There's my class:

class Time_Cal:

    def __init__(self, hour=0, minute=0, seg=0):
        self.hour = hour
        self.minute = minute
        self.seg = seg

    def __add__(self, other):
        if isinstance(other, Time_Cal):
            return self.__add_sub(other, "addition")
        else:
            raise TypeError("The object is not Time_Cal")

    def __str__(self):
        return str(self.hour) + ":" + str(self.minute) + ":" + str(self.seg)
There's the code in main:
from myTime import Time_Cal

time1 = Time_Cal(5, 45, 12)
time2 = Time_Cal(7, 50, 24)
print("The sum of Time1 and Time2:", str(time1 + time2))
The logic actually works, however, I am now testing the code what if I create my object something like this:
time1 = Time_Cal(5, 45, 12)
time2 = Time_Cal(7, 50, "24")
So time2 is not a Time_Cal object (because one of the argument is not int anymore.)
I am expecting if isinstance(other, Time_Cal) return False so, the code can raise the TypeError.
However, for some reasons, it always return True and enter the next step logic which will cause uncatched exceptions.

Is anyone who can help me out? Thanks
Reply
#2
isinstance returns True because time1 and time2 are both Time_Cal objects.
time1 = Time_Cal(5, 45, 12)
time2 = Time_Cal(7, 50, '24')
print(time1, time2)
Output:
5:45:12 7:50:24
time2 may not have a valid seconds value, but it is still a Time_Cal object. If it is important that hour, minute and seg are numbers, you should do that checking where the values are set, not where the values are used. This should cause an error, I would use a TypeError.
time2 = Time_Cal(7, 50, '24')
Maybe something like this:
    def __init__(self, hour=0, minute=0, seg=0):
        if not isinstance(hour, int, float):
            raise TypeError('hour must be a number')
Though I prefer this:
    def __init__(self, hour=0, minute=0, seg=0):
        self.hour = int(hour)
        self.minute = int(minute)
        self.seg = float(seg)
If it acts like a number treat it like a number.

Also your add function is odd. When I add 3 + 2 I don't change 3 to be 5. When I add time1 and time2 I would expect to get a new time that is their sum.
class Time_Cal:
 
    def __init__(self, hour=0, minute=0, seg=0):
        self.hour = int(hour)
        self.minute = int(minute)
        self.seg = float(seg)
        self.normalize()
 
    def normalize(self):
        while self.seg > 60:
        self.minute += self.seg // 60
        self.seg = self.seg % 60
        self.hour += self.minute // 60
        self.minute = self.minute % 60
 
    def __add__(self, other):
        return Time_Cal(
            self.hour + other.hour,
            self.minute + other.minute,
            self.seg + other.seg)

    def __str__(self):
        return str(self.hour) + ":" + str(self.minute) + ":" + str(self.seg)

time1 = Time_Cal(5, 45, 12)
time2 = Time_Cal(7, 50, '24')
print("The sum of Time1 and Time2:", time1 + time2)
Reply
#3
Thanks for the detailed explanation! Very helpful!!!


(Jul-22-2020, 06:32 PM)deanhystad Wrote: isinstance returns True because time1 and time2 are both Time_Cal objects.
time1 = Time_Cal(5, 45, 12)
time2 = Time_Cal(7, 50, '24')
print(time1, time2)
Output:
5:45:12 7:50:24
time2 may not have a valid seconds value, tut it is still Time_Cal object. If it is important that hour, minute and seg are numbers, you should do that checking where the values are set, not where the values are used. This should cause an error, I would use a TypeError.
time2 = Time_Cal(7, 50, '24')
Maybe something like this:
    def __init__(self, hour=0, minute=0, seg=0):
        if not isinstance(hour, int, float):
            raise TypeError('hour must be a number')
Though I prefer this:
    def __init__(self, hour=0, minute=0, seg=0):
        self.hour = int(hour)
        self.minute = int(minute)
        self.seg = float(seg)
If it acts like a number treat it like a number.

Also your add function is odd. When I ad 3 + 2 I don't change 3 to be 5. When I add time1 and time2 I would expect to get a new time that is their sum.
class Time_Cal:
 
    def __init__(self, hour=0, minute=0, seg=0):
        self.hour = int(hour)
        self.minute = int(minute)
        self.seg = float(seg)
        self.normalize()
 
    def normalize(self):
        while self.seg > 60:
            self.minute += 1
            self.seg -= 60
        while self.minute > 60:
            self.hour += 1
            self.minute -= 60
 
    def __add__(self, other):
        return Time_Cal(
            self.hour + other.hour,
            self.minute + other.minute,
            self.seg + other.seg)

    def __str__(self):
        return str(self.hour) + ":" + str(self.minute) + ":" + str(self.seg)

time1 = Time_Cal(5, 45, 12)
time2 = Time_Cal(7, 50, '24')
print("The sum of Time1 and Time2:", time1 + time2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 6 1,013 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  how to return a reference to an object? Skaperen 8 1,188 Jun-07-2023, 05:30 PM
Last Post: Skaperen
  declaring object parameters with type JonWayn 2 890 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,104 Jan-09-2022, 02:39 AM
Last Post: Python84
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,189 Aug-10-2021, 08:10 AM
Last Post: snippsat
Star Type Error: 'in' object is not callable nman52 3 3,383 May-01-2021, 11:03 PM
Last Post: nman52
  basic question isinstance tames 5 2,828 Nov-23-2020, 07:20 AM
Last Post: tames
  TypeError: 'type' object is not subscriptable Stef 1 4,519 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  Help with isinstance command (very simple code) Laplace12 2 2,002 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  check pandas variable type cools0607 3 6,652 Jun-12-2020, 09:28 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020