For lack of a better title, this is what I got so far.
class cordinate(int):
def __new__(self, *args, **kwargs):
self.x = args[0]
self.y = args[1]
## Try to get pixel [0,0] and see what it got:
if self.x == 0 and self.y == 0:
print('New Here')
print(self.x, self.y, self.angle, self.distance_from_edge)
self.angle = 225
self.distance_from_edge = 13
args = [self.distance_from_edge,]
return super(cordinate, self).__new__(self, *args, **kwargs)
cordinates = [cordinate(0,0), cordinate(2,10), cordinate(3,8)]
New Here
Traceback (most recent call last):
File "test.py", line 17, in <module>
cordinates = [cordinate(0,0), cordinate(2,10), cordinate(3,8)]
File "test.py", line 9, in __new__
print(self.x, self.y, self.angle, self.distance_from_edge)
AttributeError: type object 'cordinate' has no attribute 'angle'
if self.x == 2 and self.y == 10:
New Here
2 10 225 13
As you can see from the data model documentation, the first argument to __new__
is the class (conventionally cls
), not the instance (conventionally self
). Therefore you are setting class attributes on the cordinate
class (note that that's a typo, and class names should be CamelCased
) not each instance. As long as the first call to __new__
succeeds, those attributes are set on the class for all subsequent calls.
If you want to set instance attributes in __new__
, do it once you have the instance, e.g.:
class Coordinate(int):
def __new__(cls, *args, **kwargs):
self = super(Coordinate, cls).__new__(cls, *args[2:], **kwargs)
# ^ or 'inst' or whatever you like
self.x, self.y = args[:2]
...
return self