Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35649 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2012-12-27 13:33 -0700 |
| Last post | 2012-12-27 13:33 -0700 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Function Parameters Ian Kelly <ian.g.kelly@gmail.com> - 2012-12-27 13:33 -0700
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-12-27 13:33 -0700 |
| Subject | Re: Function Parameters |
| Message-ID | <mailman.1365.1356640464.29569.python-list@python.org> |
On Thu, Dec 27, 2012 at 1:16 PM, Joseph L. Casale
<jcasale@activenetwerx.com> wrote:
> When you use optional named arguments in a function, how do you deal with with
> the incorrect assignment when only some args are supplied?
>
> If I do something like:
>
> def my_func(self, **kwargs):
>
> then handle the test cases with:
>
> if not kwargs.get('some_key'):
> raise SyntaxError
> or:
>
> if kwargs.get('some_key') and kwargs.get('another_key'):
> ...
>
> I loose the introspection that some IDE's provide from the doc strings.
>
> Any ideas on how to deal with this?
Don't use kwargs for this. List out the arguments in the function
spec and give the optional ones reasonable defaults.
def my_func(self, some_key=None, another_key=None):
if some_key and another_key:
do_something()
If None is a meaningful value for the argument, then a good technique
is to use a unique object as the default instead.
MISSING = object()
def my_func(self, some_key=MISSING, another_key=MISSING):
if some_key is not MISSING and another_key is not MISSING:
do_something()
I only use kwargs myself when the set of possible arguments is dynamic
or unknown.
Back to top | Article view | comp.lang.python
csiph-web