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


Groups > comp.lang.python > #64048

Re: data validation when creating an object

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: data validation when creating an object
Date 2014-01-15 23:05 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-BA09BB.23054615012014@news.panix.com> (permalink)
References <CAOF-KfigMrCKz+O9_o6C+PBk6OB8hjOkUTWPW9PLfpobYO_kTw@mail.gmail.com> <mailman.5555.1389834993.18130.python-list@python.org>

Show all headers | View raw


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.

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 :-)

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