Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.037 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'python.': 0.02; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'foo()': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'heap': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'class.': 0.26; 'shown': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; 'ctypes': 0.31; 'file': 0.32; 'class': 0.32; 'quite': 0.32; '(most': 0.33; 'fri,': 0.33; "can't": 0.35; 'more,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'turn': 0.37; 'recent': 0.39; 'changed': 0.39; 'around.': 0.60; "you're": 0.61; 'subject:This': 0.74; '3.4': 0.84; '9:02': 0.84; 'bar:': 0.84; 'win32:': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=/6LBcXbYBAEts/H6iamnEVtXSXZGgLEbHBNc3ru08vs=; b=BmxiJJ+E5BG+5G/CQ4e9EacERnHarwdozw3VnAdAvPJdwIBnm5EDk1P8DxGG4UTKbR n3zcsmGImIVYDc+OP+L2JV3v+eQrTgFUp6wBJQxX9/GF0fhnIsIVN8dBOOAVHOkgxYlP FAGHV078h8VpTI1IQIyPYbq7NScVM26ReoZD3CEWyEJzUxAP5Nf6XX58965we8O0SiOg 6cS9o3rDPn8hYhQ0vHd59KtdT8T4HUM4D48PlS5Gsqpy6a/A0Shb48YnFR2TMh0MsiLy 2xMIJhwSOLcGYQuSTFawh7EBxsE1VEcvkUy20uhB7mEdLsXFHOnhxzLcT3L1c3JDiSh9 Rejg== MIME-Version: 1.0 X-Received: by 10.52.117.41 with SMTP id kb9mr48900vdb.97.1402010486862; Thu, 05 Jun 2014 16:21:26 -0700 (PDT) In-Reply-To: <252650940423701065.252693sturla.molden-gmail.com@news.gmane.org> References: <87tx7zi0i1.fsf@dpt-info.u-strasbg.fr> <252650940423701065.252693sturla.molden-gmail.com@news.gmane.org> Date: Fri, 6 Jun 2014 09:21:26 +1000 Subject: Re: OT: This Swift thing From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402010495 news.xs4all.nl 2872 [2001:888:2000:d::a6]:50139 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72790 On Fri, Jun 6, 2014 at 9:02 AM, Sturla Molden wrote: > You cannot spoof the type of an object in Python. Not without fiddling around. Python 3.4 on win32: >>> class Foo: def spam(self): print(self,"spams") >>> class Bar: def spam(self): print(self,"eats spam") >>> 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. However, you can't turn it into an int: >>> x.__class__ = int Traceback (most recent call last): File "", line 1, in x.__class__ = int TypeError: __class__ assignment: only for heap types So there are limits. (Obviously with ctypes you can do anything, but at that point, you're not working with Python any more, you're fiddling with CPython's RAM. That's quite different.) ChrisA