Re: data validation when creating an object

2014-01-16 Thread Robert Kern
On 2014-01-16 04:05, Roy Smith wrote: Rita writes: I know its frowned upon to do work in the __init__() method and only declarations should be there. In article , Ben Finney wrote: Who says it's frowned on to do work in the initialiser? Where are they saying it? That seems over-broad, I

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

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. Bu

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 wrote: > On 16Jan2014 15:53, Ben Finney wrote: > > Roy Smith writes: > > > Ben Finney wrote: > > > > Who says it's frowned on to do work in the initialiser? Where are > they > > > > saying it? That seems ove

Re: data validation when creating an object

2014-01-15 Thread Cameron Simpson
On 16Jan2014 15:53, Ben Finney wrote: > Roy Smith writes: > > Ben Finney 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

Re: data validation when creating an object

2014-01-15 Thread Cameron Simpson
On 16Jan2014 12:46, Chris Angelico wrote: > On Thu, Jan 16, 2014 at 12:25 PM, Cameron Simpson 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 ValueEr

Re: data validation when creating an object

2014-01-15 Thread Roy Smith
In article , Ben Finney wrote: > Roy Smith 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 fully understanding the reason why. > > Even simpler: They are mistaken in what the con

Re: data validation when creating an object

2014-01-15 Thread Ben Finney
Roy Smith writes: > Ben Finney 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 should not do > a lot of work an

Re: data validation when creating an object

2014-01-15 Thread Roy Smith
Rita writes: >> I know its frowned upon to do work in the __init__() method and only >> declarations should be there. In article , Ben Finney 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 tha

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 th

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. than

Re: data validation when creating an object

2014-01-15 Thread Chris Angelico
On Thu, Jan 16, 2014 at 12:25 PM, Cameron Simpson 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,)) > if

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 Pyth

Re: data validation when creating an object

2014-01-15 Thread Cameron Simpson
On 15Jan2014 20: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. This rule of thumb does not mea

Re: data validation when creating an object

2014-01-15 Thread Ben Finney
Rita 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 initiali

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 ca