Re: data validation when creating an object

2014-01-16 Thread Rita
Thanks everyone for the replies. On Thu, Jan 16, 2014 at 1:36 AM, Cameron Simpson c...@zip.com.au wrote: On 16Jan2014 15:53, Ben Finney ben+pyt...@benfinney.id.au wrote: Roy Smith r...@panix.com writes: Ben Finney ben+pyt...@benfinney.id.au wrote: Who says it's frowned on to do

Re: data validation when creating an object

2014-01-16 Thread Roy Smith
On Thursday, January 16, 2014 10:46:10 AM UTC-5, Robert Kern wrote: 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

Re: data validation when creating an object

2014-01-16 Thread Skip Montanaro
I suspect when best to validate inputs depends on when they come in, and what the cost is of having objects with invalid state. If the input is something that is passed along when the object is instantiated, you kind of have to validate in __init__ or __new__, right? Let's create a stupid

Re: data validation when creating an object

2014-01-16 Thread Robert Kern
On 2014-01-16 16:18, Roy Smith wrote: On Thursday, January 16, 2014 10:46:10 AM UTC-5, Robert Kern wrote: 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

Re: data validation when creating an object

2014-01-16 Thread Robert Kern
On 2014-01-16 04:05, Roy Smith wrote: Rita rmorgan...@gmail.com writes: I know its frowned upon to do work in the __init__() method and only declarations should be there. In article mailman..1389834993.18130.python-l...@python.org, Ben Finney ben+pyt...@benfinney.id.au wrote: Who

data validation when creating an object

2014-01-15 Thread Rita
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. So, should i create a function called validateData(self) inside foo? I would

Re: data validation when creating an object

2014-01-15 Thread Ben Finney
Rita rmorgan...@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

Re: data validation when creating an object

2014-01-15 Thread Cameron Simpson
On 15Jan2014 20:09, Rita rmorgan...@gmail.com wrote: 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. This rule of thumb

Re: data validation when creating an object

2014-01-15 Thread Mark Lawrence
On 16/01/2014 01:09, Rita wrote: 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. In the 10+ years that I've been using

Re: data validation when creating an object

2014-01-15 Thread Chris Angelico
On Thu, Jan 16, 2014 at 12:25 PM, Cameron Simpson c...@zip.com.au wrote: However, I would also have obvious validity checks in __init__ itself on the supplied values. Eg: def __init__(self, size, lifetime): if size 1: raise ValueError(size must be = 1, received: %r % (size,))

Re: data validation when creating an object

2014-01-15 Thread Rita
Unfortunately, I couldn't find the reference but I know I read it somewhere. Even with a selective search I wasn't able to find it. I think I read it in context of module/class test case writing. I will keep your responses in mind therefore I will put logic in __init__ for data validation.

Re: data validation when creating an object

2014-01-15 Thread Terry Reedy
On 1/15/2014 8:09 PM, Rita wrote: I know its frowned upon to do work in the __init__() method and only declarations should be there. Dear Python beginners: Don't believe the Python rules people write unless it is by one of the core developers or one of the other experts posting here. Even

Re: data validation when creating an object

2014-01-15 Thread Roy Smith
Rita rmorgan...@gmail.com writes: I know its frowned upon to do work in the __init__() method and only declarations should be there. In article mailman..1389834993.18130.python-l...@python.org, Ben Finney ben+pyt...@benfinney.id.au wrote: Who says it's frowned on to do work in the

Re: data validation when creating an object

2014-01-15 Thread Ben Finney
Roy Smith r...@panix.com writes: Ben Finney ben+pyt...@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. There are some people who advocate that C++ constructors

Re: data validation when creating an object

2014-01-15 Thread Roy Smith
In article mailman.5567.1389848051.18130.python-l...@python.org, Ben Finney ben+pyt...@benfinney.id.au wrote: Roy Smith r...@panix.com writes: 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

Re: data validation when creating an object

2014-01-15 Thread Cameron Simpson
On 16Jan2014 12:46, Chris Angelico ros...@gmail.com wrote: On Thu, Jan 16, 2014 at 12:25 PM, Cameron Simpson c...@zip.com.au wrote: However, I would also have obvious validity checks in __init__ itself on the supplied values. Eg: def __init__(self, size, lifetime): if size 1:

Re: data validation when creating an object

2014-01-15 Thread Cameron Simpson
On 16Jan2014 15:53, Ben Finney ben+pyt...@benfinney.id.au wrote: Roy Smith r...@panix.com writes: Ben Finney ben+pyt...@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