Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!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; 'python.': 0.02; 'argument': 0.05; 'advocate': 0.07; 'context': 0.07; 'conventions': 0.07; 'advice.': 0.09; 'constructor': 0.09; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'specific.': 0.09; "they've": 0.09; 'vast': 0.09; 'python': 0.11; 'c++.': 0.16; 'cons': 0.16; 'constructor.': 0.16; 'finney': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'later': 0.20; 'seems': 0.21; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'why.': 0.24; 'define': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; '(this': 0.29; 'statement': 0.30; 'work.': 0.31; 'argue': 0.31; 'commonly': 0.31; 'writes:': 0.31; 'class': 0.32; 'probably': 0.32; 'languages': 0.32; 'says': 0.33; 'beginning': 0.33; 'programmers': 0.33; "i'd": 0.34; 'advice': 0.35; 'agree': 0.35; 'classes': 0.35; 'but': 0.35; 'there': 0.35; 'c++': 0.36; 'instances': 0.36; 'largely': 0.36; 'subject:data': 0.36; 'doing': 0.36; 'should': 0.36; 'ben': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'does': 0.39; 'use.': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'new': 0.61; 'skip:* 10': 0.61; 'skip:\xe2 10': 0.65; '8bit%:40': 0.68; 'smith': 0.68; 'subject': 0.69; '8bit%:43': 0.74; 'belief': 0.84; 'mistaken': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: data validation when creating an object Date: Thu, 16 Jan 2014 15:53:56 +1100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: rasputin.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:6PfTrWHMp96/IXtJJNYEhNvGS9U= 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389848051 news.xs4all.nl 2856 [2001:888:2000:d::a6]:47080 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64050 Roy Smith writes: > Ben Finney wrote: > > > Who says it's frowned on to do work in the initialiser? Where are they > > saying it? That seems over-broad, I'd like to read the context of that > > advice. > > There are some people who advocate that C++ constructors should not do > a lot of work and/or should be incapable of throwing exceptions. The > pros and cons of that argument are largely C++ specific. […] > > But, Python is not C++. I suspect the people who argue for __init__() > not doing much are extrapolating a C++ pattern to other languages > without fully understanding the reason why. Even simpler: They are mistaken in what the constructor is named, in Python. Python classes have the constructor, ‘__new__’. I would agree with advice not to do anything but allocate the resources for a new instance in the constructor. Indeed, the constructor from ‘object’ does a good enough job that the vast majority of Python classes never need a custom constructor at all. (This is probably why many beginning programmers are confused about what the constructor is called: They've never seen a class with its own constructor!) Python instances have an initialiser, ‘__init__’. That function is for setting up the specific instance for later use. This is commonly over-ridden and many classes define a custom initialiser, which normally does some amount of work. I don't think ‘__init__’ is subject to the conventions of a constructor, because *‘__init__’ is not a constructor*. -- \ “Absurdity, n. A statement or belief manifestly inconsistent | `\ with one's own opinion.” —Ambrose Bierce, _The Devil's | _o__) Dictionary_, 1906 | Ben Finney