OT: This Swift thing
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Jun 5 23:16:34 EDT 2014
On Fri, 06 Jun 2014 09:21:26 +1000, Chris Angelico wrote:
> On Fri, Jun 6, 2014 at 9:02 AM, Sturla Molden <sturla.molden at gmail.com>
> wrote:
>> You cannot spoof the type of an object in Python.
>
> Not without fiddling around. Python 3.4 on win32:
[...]
>>>> x = Foo()
>>>> x.spam()
> <__main__.Foo object at 0x0169AB10> spams
>>>> x.__class__ = Bar
>>>> x.spam()
> <__main__.Bar object at 0x0169AB10> eats spam
>
> The thing has the same id (as shown in its repr), but has changed class.
That's not spoofing types. That is a language feature: within limits,
instances can change their type. It has been around for a long, long
time, back to Python 1.5 or older, and it has real-world use-cases:
http://code.activestate.com/recipes/68429-ring-buffer/
--
Steven D'Aprano
http://import-that.dreamwidth.org/
More information about the Python-list
mailing list