[issue28316] descriptor and repr get into conflict
Sabine Maennel
report at bugs.python.org
Fri Sep 30 02:31:55 EDT 2016
New submission from Sabine Maennel:
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 "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
File "<stdin>", line 5, in __set__
File "<stdin>", 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.
----------
files: descriptor_and_repr_error
messages: 277740
nosy: Sabine.maennel at gmail.com
priority: normal
severity: normal
status: open
title: descriptor and repr get into conflict
type: crash
versions: Python 3.5
Added file: http://bugs.python.org/file44891/descriptor_and_repr_error
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28316>
_______________________________________
More information about the Python-bugs-list
mailing list