Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64086

Re: data validation when creating an object

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'argument': 0.05; 'say,': 0.05; 'true,': 0.05; 'advocate': 0.07; 'context': 0.07; 'advice.': 0.09; 'arguments': 0.09; 'default.': 0.09; 'filename': 0.09; 'overflow': 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; 'sure,': 0.09; 'testing,': 0.09; 'python': 0.11; 'thread': 0.14; '"well,': 0.16; 'c++.': 0.16; 'cons': 0.16; 'crowd,': 0.16; 'declarations': 0.16; 'finney': 0.16; 'imo.': 0.16; 'kern': 0.16; 'limiting': 0.16; 'objects.': 0.16; 'open()': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'stringio': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; 'tends': 0.16; 'twist.': 0.16; 'underlying': 0.16; 'flexibility': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'stack': 0.19; 'seems': 0.21; '>>>': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'convenient': 0.24; 'interpret': 0.24; 'why.': 0.24; 'earlier': 0.24; "i've": 0.25; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'robert': 0.30; 'said,': 0.30; "i'm": 0.30; 'code': 0.31; '"do': 0.31; 'argue': 0.31; 'asks': 0.31; 'convenience': 0.31; 'fine,': 0.31; 'writes:': 0.31; 'file': 0.32; 'class': 0.32; 'there.': 0.32; 'languages': 0.32; 'stuff': 0.32; 'run': 0.32; 'says': 0.33; 'week.': 0.33; 'raw': 0.33; "i'd": 0.34; "can't": 0.35; 'something': 0.35; 'objects': 0.35; 'usual': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'c++': 0.36; 'largely': 0.36; 'subject:data': 0.36; 'done': 0.36; 'doing': 0.36; "didn't": 0.36; 'method': 0.36; 'possible': 0.36; 'should': 0.36; 'seconds': 0.37; 'unit': 0.37; 'being': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'most': 0.60; 'took': 0.61; 'skip:* 10': 0.61; 'making': 0.63; 'our': 0.64; 'more': 0.64; 'different': 0.65; 'spot': 0.65; 'world': 0.66; 'believe': 0.68; 'covers': 0.68; 'smith': 0.68; 'to,': 0.72; 'records': 0.73; 'other.': 0.75; 'article': 0.77; 'inform': 0.78; 'yourself': 0.78; 'eco': 0.84; 'faster.': 0.84; 'lately,': 0.84; 'rita': 0.84; 'terrible': 0.84; '5-10': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: data validation when creating an object
Date Thu, 16 Jan 2014 15:46:10 +0000
References <CAOF-KfigMrCKz+O9_o6C+PBk6OB8hjOkUTWPW9PLfpobYO_kTw@mail.gmail.com> <mailman.5555.1389834993.18130.python-list@python.org> <roy-BA09BB.23054615012014@news.panix.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 213.1.240.226
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <roy-BA09BB.23054615012014@news.panix.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5590.1389887225.18130.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389887225 news.xs4all.nl 2884 [2001:888:2000:d::a6]:35717
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64086

Show key headers only | View raw


On 2014-01-16 04:05, Roy Smith wrote:
> Rita <rmorgan466@gmail.com> writes:
>>> I know its frowned upon to do work in the __init__() method and only
>>> declarations should be there.
>
>
> In article <mailman.5555.1389834993.18130.python-list@python.org>,
>   Ben Finney <ben+python@benfinney.id.au> 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.
>
> Weird, I was just having this conversation at work earlier this week.
>
> 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.  Here's a Stack
> Overflow thread which covers most of the usual arguments on both sides:
>
> http://stackoverflow.com/questions/293967/how-much-work-should-be-done-in
> -a-constructor
>
> 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.

I'm one of those people who tends to argue this, but my limited experience with 
C++ does not inform my opinion one way or the other.

I prefer to keep my __init__() methods as dumb as possible to retain the 
flexibility to construct my objects in different ways. Sure, it's convenient to, 
say, pass a filename and have the __init__() open() it for me. But then I'm 
stuck with only being able to create this object with a true, named file on 
disk. I can't create it with a StringIO for testing, or by opening a file and 
seeking to a specific spot where the relevant data starts, etc. I can keep the 
flexibility and convenience by keeping __init__() dumb and relegating various 
smarter and more convenient ways to instantiate the object to classmethods.

Which isn't to say that "smart" or "heavy" __init__()s don't have their place 
for some kinds of objects. I just think that dumb __init__()s should be the default.

That said, what the OP asks about, validating data in the __init__() is 
perfectly fine, IMO. My beef isn't so much with the raw *amount* of stuff done 
but how much you can code yourself into a corner by making limiting assumptions. 
So from one of the "do nothing in your __init__()" crowd, I say "well, I didn't 
really mean *nothing*...."

> That being said, I've been on a tear lately, trying to get our unit test
> suite to run faster.  I came across one slow test which had an
> interesting twist.  The class being tested had an __init__() method
> which read over 900,000 records from a database and took something like
> 5-10 seconds to run.  Man, talk about heavy-weight constructors :-)

Indeed.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: data validation when creating an object Ben Finney <ben+python@benfinney.id.au> - 2014-01-16 12:16 +1100
  Re: data validation when creating an object Roy Smith <roy@panix.com> - 2014-01-15 23:05 -0500
    Re: data validation when creating an object Ben Finney <ben+python@benfinney.id.au> - 2014-01-16 15:53 +1100
      Re: data validation when creating an object Roy Smith <roy@panix.com> - 2014-01-16 00:05 -0500
    Re: data validation when creating an object Robert Kern <robert.kern@gmail.com> - 2014-01-16 15:46 +0000
      Re: data validation when creating an object Roy Smith <roy@panix.com> - 2014-01-16 08:18 -0800
        Re: data validation when creating an object Robert Kern <robert.kern@gmail.com> - 2014-01-16 16:58 +0000
    Re: data validation when creating an object Skip Montanaro <skip@pobox.com> - 2014-01-16 10:44 -0600

csiph-web