[issue28316] descriptor and repr get into conflict

2016-09-30 Thread Benjamin Peterson

Benjamin Peterson added the comment:

This is no different than this simpler case:

class A:
def __init__(self, name):
print("init {!r}".format(self))
self.name = name
def __repr__(self):
return "I am {}".format(self.name)

The instance of A doesn't not have a name attribute for __repr__ to use until 
you set it.

--
nosy: +benjamin.peterson
resolution:  -> not a bug
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28316] descriptor and repr get into conflict

2016-09-30 Thread Sabine Maennel

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 "", 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.

--
files: descriptor_and_repr_error
messages: 277740
nosy: sabine.maen...@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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com