Python Forum
Class isinstance not recognizing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class isinstance not recognizing
#1
Having a strange error. Don't know if I'm simple missing it, if I made a simple mistake, or I just don't see it.
I try to reproduce it with no luck but it exist in my project at the same place.
my Vector code
from pygame import Color
import operator

class Vector:
    # handles (x, y, z), tuple, list, pygame.Color, Vector
    def __init__(self, x, y=None, z=None):
        if y is None:
            if isinstance(x, str):
                color = Color(x)
                self.x = color.r
                self.y = color.g
                self.z = color.b
            elif isinstance(x, (tuple, list)):
                self.x, self.y, self.z = x[:3]
            elif isinstance(x, Color):
                self.x = x.r
                self.y = x.g
                self.z = x.b
            elif isinstance(x, Vector):
                self.x = x.x
                self.y = x.y
                self.z = x.z
            else:
                # Currently working around bug.
                print('Error', x)
                self.x = x.x
                self.y = x.y
                self.z = x.z
        else:
            self.x = x
            self.y = y
            self.z = z

    def tup(self):
        return self.x, self.y, self.z

    # handle basic type int, float, str
    def tup_cast(self, cast=int):
        return cast(self.x), cast(self.y), cast(self.z)

    # handle basic type int, float, str
    def cast(self, cast=int):
        self.x = cast(self.x)
        self.y = cast(self.y)
        self.z = cast(self.z)
        return self

    # overload handle Vector, Point, tuple, list, single number
    def overload(self, op, vector):
        if isinstance(vector, Vector):
            return Vector(op(self.x, vector.x), op(self.y, vector.y), op(self.z, vector.z))
        elif isinstance(vector, Point):
            return Vector(op(self.x, vector.x), op(self.y, vector.y), self.z)
        elif isinstance(vector, (tuple, list)):
            if len(vector) == 2:
                return Vector(op(self.x, vector[0]), op(self.y, vector[1]), self.z)
            return Vector(op(self.x, vector[0]), op(self.y, vector[1]), op(self.z, vector[2]))
        return Vector(op(self.x, vector), op(self.y, vector), op(self.z, vector))

    def __add__(self, vector):
        return self.overload(operator.add, vector)

    def __sub__(self, vector):
        return self.overload(operator.sub, vector)

    def __mul__(self, vector):
        return self.overload(operator.mul, vector)

    def __truediv__(self, vector):
        return self.overload(operator.truediv, vector)

    def __repr__(self):
        return "Vector({0}, {1}, {2})".format(self.x, self.y, self.z)

if __name__ == '__main__':
    colors_list = ['darkolivegreen1','chocolate','darkgoldenrod']
    fg = Color(colors_list[1])
    bg = Vector(fg) * 0.52
    print(bg)
    print(Vector(bg).cast())
where the error is coming from line 423 from my bg variable.
elif self.item in [9,10,11,12]:
            # TODO bug. Why isinstance doesn't recognize it as a Vector.
            # Vector isinstance(bg, Vector) in gradient and Vector.
            colors_list = ['darkolivegreen1','chocolate','darkgoldenrod','coral']
            fg = pygame.Color(colors_list[self.item % 4])
            bg = Vector(fg) * 0.5
            grad = gradient.horizontal((fg, bg), 30)
            letter = scene.Font.basic.render('ABCD'[self.item % 4], 1, (255,255,255))
            arect = letter.get_rect()
            arect.center = rect.center
            surface.blit(gradient.apply_surface(letter, grad), arect)
When i click on my Memory game. I have this print out.
Output:
Error Vector(127.5, 63.5, 40.0) Error Vector(101.0, 127.5, 56.0) Error Vector(127.5, 63.5, 40.0) Error Vector(105.0, 52.5, 15.0) Error Vector(92.0, 67.0, 5.5) Error Vector(105.0, 52.5, 15.0) Error Vector(101.0, 127.5, 56.0) Error Vector(92.0, 67.0, 5.5)
I just don't know why isinstance in my gradient and Vector class miss my Vector type.
Full code on Github
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
Class isinstance not recognizing - by Windspar - Dec-08-2017, 03:12 PM
RE: Class isinstance not recognizing - by Windspar - Dec-08-2017, 04:24 PM
RE: Class isinstance not recognizing - by hshivaraj - Dec-08-2017, 04:43 PM
RE: Class isinstance not recognizing - by Windspar - Dec-08-2017, 06:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sublime Text Editor not recognizing Python elroberto 5 2,968 Jun-13-2022, 04:00 PM
Last Post: rob101
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,273 Aug-10-2021, 08:10 AM
Last Post: snippsat
  basic question isinstance tames 5 2,882 Nov-23-2020, 07:20 AM
Last Post: tames
  pylint not recognizing some objects in VS Code marceloreborn 1 1,842 Jul-31-2020, 01:36 AM
Last Post: Larz60+
  Help with isinstance command (very simple code) Laplace12 2 2,039 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  isinstance() always return true for object type check Yoki91 2 2,605 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  List and isinstance Roh_80 1 2,344 Jan-14-2019, 03:52 PM
Last Post: ichabod801
  isinstance not outputting anything mikerosz94 3 3,436 Aug-31-2017, 03:07 PM
Last Post: mikerosz94
  Repost isinstance not outputting any statements mikerosz94 1 2,962 Aug-31-2017, 12:46 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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