kwargs

Use string variable **kwargs as named argument

99封情书 提交于 2019-12-23 09:25:57
问题 I am trying to figure out a way to loop through a json config file and use a key name as the argument name to a method that uses **kwargs. I have created a json config file and used key names as methods. I just append "set_" to the key name to call the correct method. I convert the json to a dictionary to loop through any of the defaults. I want to pass argument names to **kwargs by a string variable. I tried to pass a dictionary but it doesn't seem to like that. user_defaults = config[

Special use of args / kwargs

孤人 提交于 2019-12-22 09:29:20
问题 I need to do something like this: def func1(a, *args, b="BBB", **kwargs): print "a=%s b=%s args=%s kwargs=%s" % (a, b, args, kwargs) So that calling like this: func1("AAAA", h="HHH", j="JJJ") Produces the output: a=AAAA b=BBB args=() kwargs={'h': 'HHH', 'j': 'JJJ'} But it is not possible to put a default named argument after *args . ( SyntaxError: invalid syntax ) Why is this not allowed? Is there any readable way to implement this? The only way I know is b=kwargs.pop("b", "BBB") , but this

Get kwargs Inside Function

纵然是瞬间 提交于 2019-12-22 01:37:36
问题 If I have a python function like so: def some_func(arg1, arg2, arg3=1, arg4=2): Is there a way to determine which arguments were passed by keyword from inside the function? EDIT For those asking why I need this, I have no real reason, it came up in a conversation and curiosity got the better of me. 回答1: No, there is no way to do it in Python code with this signature -- if you need this information, you need to change the function's signature. If you look at the Python C API, you'll see that

python decorator to display passed AND default kwargs

我是研究僧i 提交于 2019-12-21 05:03:10
问题 I am new to python and decorators and am stumped in writing a decorator which reports not only passed args and kwargs but ALSO the unchanged default kwargs. This is what I have so far. def document_call(fn): def wrapper(*args, **kwargs): print 'function %s called with positional args %s and keyword args %s' % (fn.__name__, args, kwargs) return fn(*args, **kwargs) return wrapper @document_call def square(n, trial=True, output=False): # kwargs are a bit of nonsense to test function if not

Why does the **kwargs mapping compare equal with a differently ordered OrderedDict?

我的未来我决定 提交于 2019-12-19 15:46:31
问题 According to PEP 468: Starting in version 3.6 Python will preserve the order of keyword arguments as passed to a function. To accomplish this the collected kwargs will now be an ordered mapping . Note that this does not necessarily mean OrderedDict . In that case, why does this ordered mapping fail to respect equality comparison with Python's canonical ordered mapping type, the collections.OrderedDict : >>> from collections import OrderedDict >>> data = OrderedDict(zip('xy', 'xy')) >>> def

argparse argument order

核能气质少年 提交于 2019-12-18 18:51:29
问题 I have a little problem. I use argparse to parse my arguments, and it's working very well. To have the args, I do : p_args = parser.parse_args(argv) args = dict(p_args._get_kwargs()) But the problem with p_args is that I don't know how to get these arguments ordered by their position in the command line, because it's a dict. So is there any possibility to have the arguments in a tuple/list/ordered dict by their order in the command line? 回答1: To keep arguments ordered, I use a custom action

Proper way to use **kwargs in Python

烈酒焚心 提交于 2019-12-17 01:19:11
问题 What is the proper way to use **kwargs in Python when it comes to default values? kwargs returns a dictionary, but what is the best way to set default values, or is there one? Should I just access it as a dictionary? Use get function? class ExampleClass: def __init__(self, **kwargs): self.val = kwargs['val'] self.val2 = kwargs.get('val2') A simple question, but one that I can't find good resources on. People do it different ways in code that I've seen and it's hard to know what to use. 回答1:

Django NoReverseMatch url issue

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 02:28:06
问题 I'm getting the error "Reverse for 'recall' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'associate/recall/']" When I try to submit a form. Here is my html: <form action="{% url 'associate:recall' ordered_group %}" method="post"> {% csrf_token %} <div> <label for="recall">enter as many members of {{ ordered_group }} as you can recall </label> <input type="text" id="recall" name="recall"> </div> <div id="enter_button"> <input type="submit" value="enter"

TypeError: function(self, item, **kwargs) takes exactly 2 arguments (3 given)

∥☆過路亽.° 提交于 2019-12-12 14:36:58
问题 I have a function, which puts data into a database, called new_item() : def new_item(self, item, **optional): After sending a web form, a function should check the user input and then use this function to put the user input into the database (I'm using Flask, function name is add_item() ): Market.new_item([request.form['title'], session.get('user_id'), request.form['category']], {'desc': request.form['desc'], 'place': request.form['place'], 'price': request.form['price'], 'ono': ono}) But I

Neatly pass positional arguments as args and optional arguments as kwargs from argparse to a function

吃可爱长大的小学妹 提交于 2019-12-12 09:50:27
问题 I would like to write a Python script that takes some necessary positional and some optional command-line arguments via argparse : Let's call the positional args a , b , c , and the optional arguments x , y , z . In my Python script, I would like to pass these args on to a function; specifically, I want a , b , c to be passed as *args , and x , y , z to be passed as **kwargs , the latter retaining their names. I would like to do this many times with different functions and different numbers