Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64033
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Subject | Re: data validation when creating an object |
| Date | 2014-01-16 12:16 +1100 |
| References | <CAOF-KfigMrCKz+O9_o6C+PBk6OB8hjOkUTWPW9PLfpobYO_kTw@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5555.1389834993.18130.python-list@python.org> (permalink) |
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 | Next — Next in thread | Find similar | Unroll 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