Re: How to validate the __init__ parameters

2010-01-11 Thread Jean-Michel Pichavant
Aahz wrote: In article , Jean-Michel Pichavant wrote: class A: def __init__(self, foo = None, bar = None): if len(foo) > 5: raise ValueError('foo cannot exceed 5 characters') Bad Idea -- what happens when foo is None? You're right. That perfectly illustr

Re: How to validate the __init__ parameters

2010-01-10 Thread Aahz
In article , Jean-Michel Pichavant wrote: > >class A: >def __init__(self, foo = None, bar = None): >if len(foo) > 5: > raise ValueError('foo cannot exceed 5 characters') Bad Idea -- what happens when foo is None? -- Aahz (a...@pythoncraft.com) <*> htt

Re: How to validate the __init__ parameters

2010-01-04 Thread Albert van der Horst
In article , Chris Rebert wrote: >On Mon, Jan 4, 2010 at 10:17 AM, Albert van der Horst > wrote: > >> This triggers a question: I can see the traceback, but it >> would be much more valuable, if I could see the arguments >> passed to the functions. Is there a tool? > >print(locals()) #this actual

Re: How to validate the __init__ parameters

2010-01-04 Thread Gabriel Genellina
En Mon, 04 Jan 2010 15:17:04 -0300, Albert van der Horst escribió: This triggers a question: I can see the traceback, but it would be much more valuable, if I could see the arguments passed to the functions. Is there a tool? Yes, the cgitb module [1]. Despite its name it's a general purpose

Re: How to validate the __init__ parameters

2010-01-04 Thread Phlip
Steve Holden wrote: What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let things go wrong* when the wrong type is passed? Because s

Re: How to validate the __init__ parameters

2010-01-04 Thread Chris Rebert
On Mon, Jan 4, 2010 at 10:17 AM, Albert van der Horst wrote: > This triggers a question: I can see the traceback, but it > would be much more valuable, if I could see the arguments > passed to the functions. Is there a tool? print(locals()) #this actually gives the bindings for the entire local

Re: How to validate the __init__ parameters

2010-01-04 Thread Albert van der Horst
In article , Steve Holden wrote: > >What's the exact reason for requiring that a creator argument be of a >specific type? So operations on the instances don't go wrong? Well, why >not just admit that we don't have control over everything, and just *let >things go wrong* when the wrong type is pa

Re: How to validate the __init__ parameters

2009-12-22 Thread Lie Ryan
On 12/22/2009 8:52 PM, Bruno Desthuilliers wrote: Steve Holden a écrit : (snip) What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let t

Re: How to validate the __init__ parameters

2009-12-22 Thread r0g
Bruno Desthuilliers wrote: > Steve Holden a écrit : > (snip) >> What's the exact reason for requiring that a creator argument be of a >> specific type? So operations on the instances don't go wrong? Well, why >> not just admit that we don't have control over everything, and just *let >> things go w

Re: How to validate the __init__ parameters

2009-12-22 Thread Bruno Desthuilliers
Steve Holden a écrit : (snip) What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let things go wrong* when the wrong type is passed? va

Re: How to validate the __init__ parameters

2009-12-22 Thread Bruno Desthuilliers
Denis Doria a écrit : Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: If you use a property, you'll have the validation in the initializer AND everywhere else too. If you care about

Re: How to validate the __init__ parameters

2009-12-21 Thread Steven D'Aprano
On Tue, 22 Dec 2009 00:18:21 +, r0g wrote: > Yikes, glad to be set me straight on that one! Thanks :) It's a pity > though, I really like the way it reads. Is there anything similar with > ISN'T disabled when optimizations are turned on? Yes: an explicit test-and-raise. if not condition:

Re: How to validate the __init__ parameters

2009-12-21 Thread Steve Holden
r0g wrote: > Steven D'Aprano wrote: >> On Mon, 21 Dec 2009 21:49:11 +, r0g wrote: >> >>> I use assertions myself e.g. >>> >> foo = "123456" >> assert len(foo) <= 5 >>> Traceback (most recent call last): >>> File "", line 1, in >>> AssertionError >>> >>> >>> Dunno if this would be con

Re: How to validate the __init__ parameters

2009-12-21 Thread r0g
Steven D'Aprano wrote: > On Mon, 21 Dec 2009 21:49:11 +, r0g wrote: > >> I use assertions myself e.g. >> > foo = "123456" > assert len(foo) <= 5 >> Traceback (most recent call last): >> File "", line 1, in >> AssertionError >> >> >> Dunno if this would be considered good or bad prog

Re: How to validate the __init__ parameters

2009-12-21 Thread Steven D'Aprano
On Mon, 21 Dec 2009 21:49:11 +, r0g wrote: > I use assertions myself e.g. > foo = "123456" assert len(foo) <= 5 > Traceback (most recent call last): > File "", line 1, in > AssertionError > > > Dunno if this would be considered good or bad programming practice by > those more e

Re: How to validate the __init__ parameters

2009-12-21 Thread Lie Ryan
On 12/22/2009 4:41 AM, Denis Doria wrote: Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: class A: def __init__(self, foo, bar): self.foo = foo #check if foo is correct

Re: How to validate the __init__ parameters

2009-12-21 Thread Steven D'Aprano
On Mon, 21 Dec 2009 09:41:22 -0800, Denis Doria wrote: > Hi; > > I'm checking the best way to validate attributes inside a class. There is no "best way", since it depends on personal taste. > Of > course I can use property to check it, but I really want to do it inside > the __init__: If you

Re: How to validate the __init__ parameters

2009-12-21 Thread r0g
Denis Doria wrote: > Hi; > > I'm checking the best way to validate attributes inside a class. Of > course I can use property to check it, but I really want to do it > inside the __init__: > > class A: > def __init__(self, foo, bar): > self.foo = foo #check if foo is correct >

Re: How to validate the __init__ parameters

2009-12-21 Thread Alf P. Steinbach
* Denis Doria: I thought in something like: class A: def __init__(self, foo = None, bar = None): set_foo(foo) self._bar = bar def set_foo(self, foo): if len(foo) > 5: raise _foo = foo foo = property(setter = set_foo) But looks too much

Re: How to validate the __init__ parameters

2009-12-21 Thread Jean-Michel Pichavant
Denis Doria wrote: Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: class A: def __init__(self, foo, bar): self.foo = foo #check if foo is correct self.bar = bar Al

Re: How to validate the __init__ parameters

2009-12-21 Thread Stephen Hansen
On Mon, Dec 21, 2009 at 9:41 AM, Denis Doria wrote: > All examples that I saw with property didn't show a way to do it in > the __init__. Just to clarify, I don't want to check if the parameter > is an int, or something like that, I want to know if the parameter do > not use more than X chars; an

How to validate the __init__ parameters

2009-12-21 Thread Denis Doria
Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: class A: def __init__(self, foo, bar): self.foo = foo #check if foo is correct self.bar = bar All examples that I saw