classification
Title: __class__ assignment: new-style? heap? == confusing
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Trundle, benjamin.peterson, eric.araujo, flox, jonash, stutzbach, terry.reedy
Priority: normal Keywords: patch

Created on 2008-12-08 21:25 by terry.reedy, last changed 2012-04-17 17:12 by flox.

Files
File name Uploaded Description Edit
4600.diff jonash, 2011-02-27 18:00
Messages (4)
msg77343 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-12-08 21:25
ob.__class__ = ob2
gives some confusing TypeError messages.

>>> c.__class__ = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ must be set to new-style class, not 'int' object

Problem: 'new-style' is obsolete in 3.0. It is also too inclusive...

>>> c.__class__ = object
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: only for heap types

object *is* 'new-style'.  I presume 'heap type' means 'class defined by
class statement'.  If so, let us say so, since beginning programmers may
not know what a 'heap type' is.  If the above is incorrect, then this
experienced programmer also does not know what it means in Python
context ;-).

Proposal: when someone tries to set __class__ to an inappropriate
object, give similar error message for instances and heap classes.

TypeError: __class__ must be set to a class defined by a class
statement, not 'xxx' [object].

where 'object', without the brackets, is added for non-classes, as it is
today.

>>> c.__class__ = object
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: only for heap types

C, the class of c, *is* a heap type.  The different problem, the target
being an instance of heap class, should get a different message.  'Heap'
is still possibly confusing. Proposal:

TypeError: __class__ assignment: only for instances of classes defined
by class statements.
msg77381 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-12-09 00:59
Related issue that applies to recent 2.x.  Language/data model/Objects,
values, and types says 'An object’s type is also unchangeable.'.  This
should be changed or deleted.
msg129643 - (view) Author: Jonas H. (jonash) * Date: 2011-02-27 18:00
Here comes a patch, changing the behaviour to:

./python -q
>>> class C:
...   pass
... 
>>> (1).__class__ = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ must be set to a class defined by a class statement, not 'int' object
>>> (1).__class__ = object
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: class__ must be set to a class defined by a class statement, not 'object'
>>> (1).__class__ = C
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: only for instances of classes defined by class statements
msg129646 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-02-27 18:25
This is not really accurate:

>>> class x(int): pass
... 
>>> class y(object): pass
... 
>>> x().__class__ = y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: 'x' object layout differs from 'y'
>>> y().__class__ = x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: 'y' object layout differs from 'x'
History
Date User Action Args
2012-04-17 17:12:08floxsetnosy: + flox
2011-05-23 11:32:59eric.araujosetassignee: docs@python ->

nosy: - docs@python
2011-05-23 11:32:37eric.araujosetnosy: + eric.araujo

versions: + Python 3.3, - Python 3.0
2011-02-27 18:25:42benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg129646
2011-02-27 18:00:31jonashsetfiles: + 4600.diff

nosy: + jonash
messages: + msg129643

keywords: + patch
2011-02-27 16:14:25Trundlesetnosy: + Trundle
2011-02-27 16:04:58stutzbachsetnosy: + stutzbach
2010-08-01 06:40:22georg.brandlsetassignee: docs@python

nosy: + docs@python
2008-12-09 00:59:47terry.reedysetmessages: + msg77381
2008-12-08 21:25:22terry.reedycreate