Re: assertions to validate function parameters

2007-01-27 Thread Steven D'Aprano
On Sat, 27 Jan 2007 19:29:06 -0800, Carl Banks wrote: > On Jan 27, 6:51 pm, Steven D'Aprano > <[EMAIL PROTECTED]> wrote: >> On Sat, 27 Jan 2007 06:58:04 -0800, Carl Banks wrote: >> >> I find that when I detect invalid parameters overtly, I spend less time >> >> debugging. >> >> > If it helps go a

Re: assertions to validate function parameters

2007-01-27 Thread Carl Banks
On Jan 27, 6:51 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Sat, 27 Jan 2007 06:58:04 -0800, Carl Banks wrote: > >> I find that when I detect invalid parameters overtly, I spend less time > >> debugging. > > > If it helps go ahead an use them. The world won't end if you use an > > asser

Re: assertions to validate function parameters

2007-01-27 Thread Steven D'Aprano
On Sat, 27 Jan 2007 06:58:04 -0800, Carl Banks wrote: >> I find that when I detect invalid parameters overtly, I spend less time >> debugging. > > If it helps go ahead an use them. The world won't end if you use an > assertion in a less than ideal situation. And, after all, if someone > doesn

Re: assertions to validate function parameters

2007-01-27 Thread Carl Banks
On Jan 25, 11:54 am, Matthew Wilson <[EMAIL PROTECTED]> wrote: > Lately, I've been writing functions like this: > > def f(a, b): > > assert a in [1, 2, 3] > assert b in [4, 5, 6] > > The point is that I'm checking the type and the values of the > parameters. > > I'm curious how this does

Re: assertions to validate function parameters

2007-01-27 Thread Carl Banks
On Jan 25, 11:26 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > Note also that for real code, a bare assert like that is uselessly > uninformative: > > >>> x = 1 > >>> assert x == 3Traceback (most recent call last): > File "", line 1, in ? > AssertionError In real code, a traceback usually

Re: assertions to validate function parameters

2007-01-27 Thread Nick Craig-Wood
Matthew Woodcraft <[EMAIL PROTECTED]> wrote: > I have a question for you. Consider this function: > > def f(n): > """Return the largest natural power of 2 which does not exceed n.""" > if n < 1: > raise ValueError > i = 1 > while i <= n: > j = i > i

Re: assertions to validate function parameters

2007-01-26 Thread Steven D'Aprano
On Fri, 26 Jan 2007 18:28:32 +, Matthew Woodcraft wrote: > I have a question for you. Consider this function: > > def f(n): > """Return the largest natural power of 2 which does not exceed n.""" > if n < 1: > raise ValueError > i = 1 > while i <= n: > j = i >

Re: assertions to validate function parameters

2007-01-26 Thread Matthew Woodcraft
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > The less your function does, the more constrained it is, the less > testing you have to do -- but the less useful it is, and the more work > you put onto the users of your function. Instead of saying something > like > a = MyNumericClass(1) > b = MyNum

Re: assertions to validate function parameters

2007-01-25 Thread Steven D'Aprano
On Thu, 25 Jan 2007 16:54:05 +, Matthew Wilson wrote: > Lately, I've been writing functions like this: > > def f(a, b): > > assert a in [1, 2, 3] > assert b in [4, 5, 6] > > The point is that I'm checking the type and the values of the > parameters. If somebody passes in a == MyNum

Re: assertions to validate function parameters

2007-01-25 Thread George Sakkis
On Jan 25, 11:54 am, Matthew Wilson <[EMAIL PROTECTED]> wrote: > Lately, I've been writing functions like this: > > def f(a, b): > > assert a in [1, 2, 3] > assert b in [4, 5, 6] > > The point is that I'm checking the type and the values of the > parameters. > > I'm curious how this does o

assertions to validate function parameters

2007-01-25 Thread Matthew Wilson
Lately, I've been writing functions like this: def f(a, b): assert a in [1, 2, 3] assert b in [4, 5, 6] The point is that I'm checking the type and the values of the parameters. I'm curious how this does or doesn't fit into python's duck-typing philosophy. I find that when I detect inv