Python Forum

Full Version: Class isinstance not recognizing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
I did another test. Types.
print(repr(type(x)), repr(type(self)))
Output:
<class 'PyScene.tool.point.Vector'> <class 'tool.point.Vector'> Error Vector(105.0, 52.5, 15.0)
Would this effect the isinstance ?
If so how would i fix it ?
Im not sure if you already have a vector module installed in your environment? If so that might contradict with the your Vector class.
Can you try changing the name of the your class from Vector to something else and check if you see similar behaviour?

This is just an experiments, I dont have explanation for this peculiar behavior.
Okay. I figure it out. It from a bad import.
In my gradient.py.
from tool.point import Vector # had this
from .point import Vector # when it should have been this