Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'configure': 0.05; 'elif': 0.05; '"""': 0.07; 'preference': 0.07; 'pypi': 0.07; '(aka': 0.09; '__init__': 0.09; 'arguments': 0.09; 'created,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'obj': 0.09; 'override': 0.09; '~ethan~': 0.09; 'python': 0.11; 'def': 0.12; '*args):': 0.16; '__init__,': 0.16; '__new__': 0.16; 'args:': 0.16; 'args[0]': 0.16; 'docstring': 0.16; 'docstrings': 0.16; 'easier.': 0.16; 'enum': 0.16; 'immutable,': 0.16; 'len(args)': 0.16; 'skip:= 80': 0.16; 'subclass': 0.16; 'subclassing': 0.16; 'subject:when': 0.16; 'w/o': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'split': 0.19; 'header:User- Agent:1': 0.23; "aren't": 0.24; 'specify': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; 'raise': 0.29; 'member.': 0.30; 'along': 0.30; 'code': 0.31; 'class': 0.32; 'handled': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'advice': 0.35; 'but': 0.35; 'really': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'example,': 0.37; 'members.': 0.37; 'two': 0.37; 'easily': 0.37; 'starting': 0.37; 'step': 0.37; 'handle': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'new': 0.61; 'numbers': 0.61; 'received:173': 0.61; 'personal': 0.63; 'more': 0.64; 'different': 0.65; 'between': 0.67; '3000': 0.68; '100': 0.79; 'dealt': 0.91; 'why?': 0.91 Date: Mon, 14 Oct 2013 16:21:38 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: when to use __new__, when to use __init__ References: <4ccd060c-0ab7-4f8a-bc19-f6eeaaf17bc9@googlegroups.com> In-Reply-To: <4ccd060c-0ab7-4f8a-bc19-f6eeaaf17bc9@googlegroups.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator3304.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:54678 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20= 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381794392 news.xs4all.nl 15882 [2001:888:2000:d::a6]:35302 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56832 On 10/14/2013 03:07 PM, Peter Cacioppi wrote: > I've dome some reading on the difference between __new__ and __init__, and never really groked it. I just followed the advice that you should almost always use __init__. Object creation in Python is a two step process: - create the object (aka __new__, and make sure you return the new object! ;) - configure the object (aka __init__) If the object is immutable, everything has to be done in __new__. If the object is mutable, then you should split your code along the creation/configuration guidelines of __new__ and __init__, even though you could do it all in __new__. Why? To make subclassing easier. As an example, consider the new Enum[1] data type: my personal preference is to not specify the numbers, and to have docstrings on the Enum members. In order to achieve this I have to override __new__ as that is when the class structures are created, but I set the docstring in __init__: ================================================================================== class AutoEnum(Enum): """ Automatically numbers enum members starting from 1. Includes support for a custom docstring per member. """ __last_number__ = 0 def __new__(cls, *args): """Ignores arguments (will be handled in __init__.""" value = cls.__last_number__ + 1 cls.__last_number__ = value obj = object.__new__(cls) obj._value_ = value return obj def __init__(self, *args): """Can handle 0 or 1 argument; more requires a custom __init__. 0 = auto-number w/o docstring 1 = auto-number w/ docstring 2+ = needs custom __init__ (don't call this __init__) """ if len(args) == 1 and isinstance(args[0], (str, unicode)): self.__doc__ = args[0] elif args: raise TypeError('%s not dealt with -- need custom __init__' % (args,)) ================================================================================== Now, if I need some other Enum class with auto-numbering, but different arguments I can easily subclass AutoEnum: ================================================================================= class Rounds(AutoEnum): def __init__(self, x_length, y_length): self.x_length = x_length self.y_length = y_length SMALL_CICRLE = 100, 100 LARGE_ELLIPSE = 5000, 3000 ================================================================================= [1] enum34 is available on PyPI if you aren't able to move to Python3.4. -- ~Ethan~