Class isinstance not recognizing - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Class isinstance not recognizing (/thread-6796.html) |
Class isinstance not recognizing - Windspar - Dec-08-2017 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. I just don't know why isinstance in my gradient and Vector class miss my Vector type.Full code on Github RE: Class isinstance not recognizing - Windspar - Dec-08-2017 I did another test. Types. print(repr(type(x)), repr(type(self))) Would this effect the isinstance ?If so how would i fix it ? RE: Class isinstance not recognizing - hshivaraj - Dec-08-2017 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. RE: Class isinstance not recognizing - Windspar - Dec-08-2017 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 |