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


Groups > comp.lang.python > #64033

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!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'context': 0.07; 'advice.': 0.09; 'pep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spaces': 0.09; 'def': 0.12; '8bit%:32': 0.16; 'declarations': 0.16; 'enough.': 0.16; 'finney': 0.16; 'foo(object):': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:object': 0.16; 'subject:when': 0.16; ':-)': 0.16; 'seems': 0.21; 'preferred': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'tend': 0.24; 'class.': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'men': 0.31; 'way?': 0.31; 'writes:': 0.31; 'class': 0.32; 'there.': 0.32; 'probably': 0.32; 'run': 0.32; 'says': 0.33; 'entirely': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; "i'd": 0.34; 'advice': 0.35; 'but': 0.35; 'there': 0.35; 'functions.': 0.36; 'subject:data': 0.36; 'done': 0.36; 'doing': 0.36; 'method': 0.36; 'should': 0.36; 'so,': 0.37; 'too': 0.37; 'two': 0.37; 'being': 0.38; 'ben': 0.38; 'depends': 0.38; 'to:addr :python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'called': 0.40; 'skip:x 10': 0.40; 'read': 0.60; "you're": 0.61; 'name': 0.63; 'hear': 0.63; 'our': 0.64; 'skip:\xe2 10': 0.65; 'surrounding': 0.68; 'much,': 0.84; 'rita': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: data validation when creating an object
Date Thu, 16 Jan 2014 12:16:19 +1100
References <CAOF-KfigMrCKz+O9_o6C+PBk6OB8hjOkUTWPW9PLfpobYO_kTw@mail.gmail.com>
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 <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:IRx/q5tySen//C57HpKGoyglRqM=
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.5555.1389834993.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389834993 news.xs4all.nl 2830 [2001:888:2000:d::a6]:58632
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64033

Show key headers only | View raw


Rita <rmorgan466@gmail.com> writes:

> I would like to do some data validation when its going to a class.
>
> class Foo(object):
>   def __init__(self):
>     pass
>
> I know its frowned upon to do work in the __init__() method and only
> declarations should be there.

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.

> So, should i create a function called validateData(self) inside foo?

If you're going to create it, ‘validate_data’ would be a better name
(because it's PEP 8 conformant).

> I would call the object like this
>
> x=Foo()
> x.validateData()

You should also be surrounding the “=” operator with spaces (PEP 8
again) for readability.

> Is this the preferred way? Is there a way I can run validateData()
> automatically, maybe put it in __init__?

It depends entirely on what is being done in those functions.

But in general, we tend not to write our functions small enough or
focussed enough. So general advice would be that, if you think the
function is going to be too long and/or doing too much, you're probably
right :-)

-- 
 \         “Nature hath given men one tongue but two ears, that we may |
  `\          hear from others twice as much as we speak.” —Epictetus, |
_o__)                                                      _Fragments_ |
Ben Finney

Back to comp.lang.python | Previous | NextNext 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