Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33148

Re: List comprehension for testing **params

From Terry Reedy <tjreedy@udel.edu>
Subject Re: List comprehension for testing **params
Date 2012-11-11 18:37 -0500
References <50a0258f$0$21241$ba4acef3@reader.news.orange.fr> <CALwzidnkFqk1+UOvCzbQNC_ootr+fJWCXE-j8WaSg2fQ-fsDiA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3565.1352677051.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 11/11/2012 5:56 PM, Ian Kelly wrote:
> On Sun, Nov 11, 2012 at 3:24 PM, Cantabile <cantabile.03@wanadoo.fr> wrote:
>> I'd like to do something like that instead of the 'for' loop in __init__:
>>
>> assert[key for key in required if key in params.keys()]
>
> A list evaluates as true if it is not empty.  As long as at least one
> of the required parameters is present, the list will not be empty and
> will evaluate as true, and the assertion will pass.  Try this instead:
>
> assert all(key in params.keys() for key in required)
>
> By the way, using assertions for input checking is generally not
> considered good practice.   As soon as Python is invoked with the -O
> option, your input testing is tossed out the window.  Assertions are
> good to use for internal logic testing, to avoid unexpected state.
> For input checking, use an explicit if test and exception:
>
> if any(key not in params.keys() for key in required):
>      raise ValueError("Missing required keyword argument")
>
> Additionally, if the arguments are required, then they probably have
> no business being wrapped up in **params in the first place.  Why not
> just define your method like:
>
> def __init__(self, smtp, login, subject, from, to, msg):

or if you want them to be identified by keyword only (since 7 positional 
args is a bit much)

def __init__(self, smtp, login, *, subject, from, to, msg):

(I forget when this feature was added)


>      # ...
>
> And then Python will do all the work of requiring them to be present for you.
>


-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

List comprehension for testing **params Cantabile <cantabile.03@wanadoo.fr> - 2012-11-11 23:24 +0100
  Re: List comprehension for testing **params Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-11 15:56 -0700
  Re: List comprehension for testing **params Tim Chase <python.list@tim.thechases.com> - 2012-11-11 16:49 -0600
  Re: List comprehension for testing **params Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-11 23:18 +0000
    Re: List comprehension for testing **params Tim Chase <python.list@tim.thechases.com> - 2012-11-11 18:21 -0600
      Re: List comprehension for testing **params Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-12 07:32 +0000
  Re: List comprehension for testing **params Terry Reedy <tjreedy@udel.edu> - 2012-11-11 18:37 -0500
    Re: List comprehension for testing **params Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-11 23:41 +0000
  Re: List comprehension for testing **params Cantabile <cantabile.03@wanadoo.fr> - 2012-11-12 00:53 +0100
  Re: List comprehension for testing **params Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-11-12 11:48 +0100
    Re: List comprehension for testing **params Cantabile <cantabile.03@wanadoo.fr> - 2012-11-12 23:25 +0100

csiph-web