[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

OT: This Swift thing

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:


Steven D'Aprano