Message289426
Your code changes __class__, not type, which would remain equal to
"instance". (my understanding, could be wrong). The docs say the following:
https://docs.python.org/3.7/reference/datamodel.html
> Like its identity, an object’s type is also unchangeable. [1]
>
> [1] It is possible in some cases to change an object’s type, under
certain controlled conditions.
> It generally isn’t a good idea though, since it can lead to some very
strange behavior if it is
> handled incorrectly.
So I think it's safe to assume that type doesn't change; if you change
type, you get undefined ("very strange") behavior. Based on the comment in
the footnote, other code clearly assumes that type doesn't change, so I
don't see why list.sort() should be any different.
On Fri, Mar 10, 2017 at 5:08 PM ppperry <report@bugs.python.org> wrote:
>
> ppperry added the comment:
>
> Does this work with wacky code like this?
> @functools.total_ordering
> class ClassAssignmentCanBreakChecks():
> def __init__(self, i):
> self._i = i
> def __lt__ (self, other):
> last.__class__ = OrdinaryOldInteger
> return self._i < (other._i if hasattr(other, '_i')
> else other)
> @functools.total_ordering
> class OrdinaryOldInteger:
> def __init__(self, i):
> self._i = i
> def __lt__(self, other):
> return self._i < (other._i if hasattr(other, '_i')
> else other)
> lst = [ClassAssignmentCanBreakChecks(i) for i in range(10)]
> random.shuffle(lst)
> last = lst[-1]
> lst.sort()
> It looks like it will initially say that all are the same type, and
> attempt that optimization, which will probably lead to unexpected results
> as that condition is no longer true after the first compare.
>
> ----------
> nosy: +ppperry
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue28685>
> _______________________________________
> |
|
Date |
User |
Action |
Args |
2017-03-11 00:31:27 | elliot.gorokhovsky | set | recipients:
+ elliot.gorokhovsky, tim.peters, vstinner, serhiy.storchaka, ppperry, mdk |
2017-03-11 00:31:27 | elliot.gorokhovsky | link | issue28685 messages |
2017-03-11 00:31:27 | elliot.gorokhovsky | create | |
|