python - Method ignored when subclassing a type defined in a C module -
i subclassing type defined in c module alias attributes , methods script works in different contexts.
how work, have tweak dictionary of class manually ? if don't add reference distanceto
in dictionnary, point3d has no attribute named distanceto
.
class point3d(app.base.vector): def __new__(cls, x, y, z): obj = super(point3d, cls).__new__(cls) obj.x, obj.y, obj.z = x, y, z obj.__dict__.update({ 'x':property(lambda self: self.x), 'y':property(lambda self: self.y), 'z':property(lambda self: self.z), 'distanceto':lambda self, p: self.distancetopoint(p)}) return obj def distanceto(self, p): return self.distancetopoint(p)
i thinking once __new__
had returned instance still populate methods , attributes. can shed light on ?
edit : module import freecad. c base type defined there. vector derived form definition here
edit2 : tried following :
class point3d(app.base.vector): def __new__(cls, x, y, z): obj = super(point3d, cls).__new__(cls) obj.x, obj.y, obj.z = x, y, z obj.__dict__.update({ 'x': x, 'y': y, 'z': z, 'distanceto':lambda self, p: self.distancetopoint(p)}) return obj def distanceto(self, p): return self.distancetopoint(p)
and after creating second point, both point3d p
returns value of last point p.x
, p.y
, p.z
no matter x,y,z
parameters passed @ creation of instance. p.x, p.y, p.z
return expected values. seems indicate dictionary shared between instances.
edit 3 : problem solved ! py_tpflags_basetype bit set 0 prevent subclassing explained in answer below.
i don't understand why want add properties dynamically. use:
class point3d(app.base.vector): def __init__(self, x, y, z): super().__init__(x, y, z) # or maybe super().__init__([x, y, z]) @property def x(self): return self[0] # guessing app.base.vector works list @property.setter def x(self, value): self[0] = value # y , z likewise.
Comments
Post a Comment