Issue 28316: descriptor and repr get into conflict (original) (raw)
I was working with descriptors and hit on an error, that I do not understand. I attach a protocol of my interactive python session that will show you what happened and the session is reproducible:
I had a class employing a descriptor: class Descriptor(object): def init(self, name): self.name = name def set(self, instance, val): print('Updating', self.name, 'for', instance) instance.dict[self.name] = val
class A: x = Descriptor('x') def init(self, name, x): self.x = x self.name = name def repr(self): return "I am {}".format(self.name)
if defined like this it hits an error when I want to initialize it: a = A('a', 2) Traceback (most recent call last): File "", line 1, in File "", line 4, in init File "", line 5, in set File "", line 7, in repr AttributeError: 'A' object has no attribute 'name' Updating x for
The error could be fixed by just exchanging the assignments in the init of A: class A: x = Descriptor('x') def init(self, name, x): self.name = name self.x = x def repr(self): return "I am {}".format(self.name) Now a = A('a', 2) works as expected. But this seems weird to me.