Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Yann Kaiser Newsgroups: comp.lang.python Subject: Re: A problem with classes - derived type Date: Mon, 09 May 2016 06:51:53 +0000 Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de f1FY6kgFDRYHES8g639jXQizZjt1EjUGrVWO0vFVwHZQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'value,': 0.03; 'kaiser': 0.07; 'derived': 0.09; 'res': 0.09; 'url:github': 0.09; 'def': 0.13; '...)': 0.16; '2016': 0.16; 'paulo': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'require.': 0.16; 'subject:type': 0.16; 'wrote:': 0.16; 'pfxlen:0': 0.18; 'thanks.': 0.18; 'to:2**1': 0.21; 'object.': 0.22; 'subject:problem': 0.22; 'suppose': 0.22; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'sense': 0.26; 'message-id:@mail.gmail.com': 0.27; 'about.': 0.29; 'url:mailman': 0.30; "can't": 0.32; 'run': 0.33; 'class': 0.33; 'url:python': 0.33; 'url:listinfo': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'url:org': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'method': 0.37; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'side': 0.62; 'email addr:gmail.com': 0.62; 'dark': 0.84; 'object:': 0.84; 'or:': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=D+hG08WJfGGHoWsBOX0QpM24zm6N3TjsT35pfQTjMJU=; b=L4x6rrlq3NA+rfqdF3m3vLn2LZ01fyTGonylOKrq/+JSo8xFQt14n0r6VsptQhg3vR X2oFUMw5c4Hvv/q6NxmLvoBRY8EQdRlxlouXw4Y7n6S8jGRBQ5ml2DJr9qXPEGgAQBX7 04RwWPAB2ghNkYuZYkGCFoadspwwj9DH1ahWtz4+VZKMHTjDopAm0xIv/WyBo+z5/smq iHr7Yo6vmeD/j5cNxohzA9YIX4EOp65LJglqEzVzpgWzll3ZTY9xsqUYwNphOT+XbpFM 2d/BKk2ov0R5YRX1zyJ+1TYPXk8KlnJfLmMvdNAzxk6WC+zq3bhkl2FVRHDGsiKdo0BX erPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=D+hG08WJfGGHoWsBOX0QpM24zm6N3TjsT35pfQTjMJU=; b=YXmpnxeGZdx706e0HbbIB/2LQpIz3j0EIcp65dAiyd8YsFXtUwyS6sXh/jMRLXwNah 14/Qa4djEMUrFWuKeaUv26XIBJouExPOn56Z/q1qEf5oUJD0ukB2T7xHRVWQs+S4xC8m e84peJ9vmNC7XTrUsCfeY5n2lMnFLkFiBTQ+mPpP4H0UMpBmq6VVre3OJTPw7B6OBmS3 neGHTECnunv2/6fOS0wEE228U31zudlJLhTPjPxQ8Obn0YvhUPW6LyOIBcC1ZFsHfexp mvP8ZVPjvoN/KgiThHah6f0pcHvHz/TJiwf9rsjcTOa9JL1HJeMnfHEqTgfKaQYfzy17 2nRw== X-Gm-Message-State: AOPr4FUDlOg22qhgnxFIAqWrellSIfK85zvTcLjNds0YEBIiiM4QpVmrX68WONN/erHzVNOtzVZuyNnM2L3lNg== X-Received: by 10.28.230.137 with SMTP id e9mr10016723wmi.0.1462776723039; Sun, 08 May 2016 23:52:03 -0700 (PDT) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:108416 If you can't change A to use something like "type(self)(...)" to create its return value, you could use the dark side and swap res's __class__: res.__class__ = B Or: res.__class__ = type(self) Do note that B.__init__ will not be run when you do this, so it is up to you to execute any additional initialization B might require. Alternatively if it makes sense for you you can call B's methods on an A object like so: B.b_method(a_object, ...) On Mon, 9 May 2016 at 06:26 Paulo da Silva wrote: > Hi! > > Suppose I have a class A whose implementation I don't know about. > That class A has a method f that returns a A object. > > class A: > ... > def f(self, <...>): > ... > > Now I want to write B derived from A with method f1. I want f1 to return > a B object: > > class B(A): > ... > def f1(self, <...>): > ... > res=f(<...>) > > How do I return res as a B object? > > Thanks. > -- > https://mail.python.org/mailman/listinfo/python-list > -- Yann Kaiser kaiser.yann@gmail.com yann.kaiser@efrei.net +33 6 51 64 01 89 https://github.com/epsy