Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: What arguments are passed to the __new__ method ? Date: Wed, 2 Mar 2016 11:26:30 -0700 Lines: 31 Message-ID: References: <56d5d043$0$632$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 1UbC1oMYZKNvnuykpGniRw2zP5EuXl4aIaX8EPLX/RSQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'method.': 0.05; 'override': 0.07; '__init__': 0.09; 'ast': 0.09; 'immutable': 0.09; 'subject:method': 0.09; 'python.': 0.11; 'def': 0.13; 'wed,': 0.15; '2016': 0.16; '__new__': 0.16; 'called,': 0.16; 'constructor.': 0.16; 'personally,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subclassing': 0.16; 'write.': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'arguments': 0.22; 'pass': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; "i've": 0.25; 'define': 0.27; 'subject: ?': 0.27; 'message- id:@mail.gmail.com': 0.27; 'tend': 0.27; 'code:': 0.29; 'typically': 0.29; 'classes': 0.30; 'skip:_ 10': 0.32; 'maybe': 0.33; 'class': 0.33; 'tue,': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'clear': 0.35; 'needed': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'received:209': 0.38; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'different': 0.63; 'mar': 0.65; 'here': 0.66; 'potentially': 0.67; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=6a0hDRtEWMk401mXkddlZuJ8K9+WTgeqFdB6DGzRc6o=; b=pUe/sAvb2nhlT6nn3K97PGTDTgKI9bSmRcljZwcFRBGsBrY42LzQ3IwM0AKzi/oBKx 3MKev9AyxcFkqP20pSL3A8wMA0fcBtF1Bf2JNMe1aO+MJQWEXeqFTLGWc/8Awv/Dnrwi WKG7G165I65JyPi4Y2qg5DvtvFUbi37k4gPMSoGB2bL5czGOLhY20dL+blUluKx9GVee +fymMg8oSulUGPOIbHN3hXZPeAtNhiFZgs8BcBGS/oyrO0zvGgY1LMoJYLX5KBJP9CZR 2zLE8bemDSxLhJLDoc85g51iF3CFpsr/kBiD0sH7sqUZtfrirbu8bM7CiPD0Fps4IvSJ 2ilQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=6a0hDRtEWMk401mXkddlZuJ8K9+WTgeqFdB6DGzRc6o=; b=R0MnDTAKGjDY3f2FU5LA2LDzXaDVRwDpzwLvwoEUdxn3LBT1enFcQIRFZczJqBGIpd yqVwj8D9FaLvjTUgjYzn6+eWqZDdRx6M0aODqd4sGae1+sDC0QqEEckqwIM0Y+4cD/Qv pnWRY4AW2An8uIeEIm5gCbwTCwpuYho9Hr0SkrMxsIOVKQhV5Nfa5jOXAmucuYLBk113 XpOaIl3ByMG87HKAjqSqgXQUEbSoayfrcNMudpBalY8v38dTOMAQLs/aKpqOhuBrM865 IhKvoAQCw7s7qVNraNLJ+PgYc6uZVDok0y0CLwe8YIcr/VHijsScviSxDNsomvV3HVUB U9dQ== X-Gm-Message-State: AG10YOT+H/lp60r/vf56IIY4dyBIMiAo6KWIRidx1Br0WO78aWS5/RbidTDVf6QSFYGg9nLD7stLI6ANthNcnw== X-Received: by 10.107.11.93 with SMTP id v90mr31147945ioi.188.1456943229843; Wed, 02 Mar 2016 10:27:09 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103892 On Wed, Mar 2, 2016 at 10:57 AM, Rob Gaddi wrote: > Peter Pearson wrote: > >> On Tue, 1 Mar 2016 18:24:12 +0100, ast wrote: >>> >>> It's not clear to me what arguments are passed to the >>> __new__ method. Here is a piece of code: >>> >>> >>> class Premiere: >>> >>> def __new__(cls, price): >>> return object.__new__(cls) >>> >>> def __init__(self, price): >>> pass >> [snip] >> >> Of course, maybe you don't need to define a __new__ method at all. >> Personally, I find that __init__ suffices for my simple needs. >> > > I tend to need __init__ on about half of the classes I write. I think > I've needed __new__ all of twice in the years I've been writing Python. Typically there are only two reasons to override __new__: you potentially want to return an object of a different class than the class that was called, or you're subclassing an immutable type and need to handle the superclass arguments before they get passed to the constructor.