Questions tagged [keyword-argument]

Keyword argument enable you to specify an argument for a particular argument by associating the argument with the argument's name rather than with the argument's position in the argument list.

Keyword arguments are a synonym for named-parameters

821 questions
1476
votes
11 answers

Use of *args and **kwargs

So I have difficulty with the concept of *args and **kwargs. So far I have learned that: *args = list of arguments - as positional arguments **kwargs = dictionary - whose keys become separate keyword arguments and the values become values of these…
MacPython
  • 17,031
  • 10
  • 38
  • 46
794
votes
13 answers

What is the purpose and use of **kwargs?

What are the uses for **kwargs in Python? I know you can do an objects.filter on a table and pass in a **kwargs argument.   Can I also do this for specifying time deltas i.e. timedelta(hours = time1)? How exactly does it work? Is it classes as…
Federer
  • 30,291
  • 37
  • 89
  • 120
495
votes
14 answers

Proper way to use **kwargs in Python

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…
Kekoa
  • 26,038
  • 13
  • 69
  • 90
371
votes
3 answers

Converting Python dict to kwargs?

I want to build a query for sunburnt(solr interface) using class inheritance and therefore adding key - value pairs together. The sunburnt interface takes keyword arguments. How can I transform a dict ({'type':'Event'}) into keyword arguments…
teaforthecat
  • 3,941
  • 2
  • 14
  • 8
127
votes
6 answers

How To Check If A Key in **kwargs Exists?

Python 3.2.3. There were some ideas listed here, which work on regular var's, but it seems **kwargs play by different rules... so why doesn't this work and how can I check to see if a key in **kwargs exists? if kwargs['errormessage']: print("It…
Zamphatta
  • 4,175
  • 8
  • 26
  • 32
103
votes
4 answers

Passing a list of kwargs?

Can I pass a list of kwargs to a method for brevity? This is what i'm attempting to do: def method(**kwargs): #do something keywords = (keyword1 = 'foo', keyword2 = 'bar') method(keywords)
jwanga
  • 3,786
  • 4
  • 24
  • 24
73
votes
8 answers

Why use **kwargs in python? What are some real world advantages over using named arguments?

I come from a background in static languages. Can someone explain (ideally through example) the real world advantages of using **kwargs over named arguments? To me it only seems to make the function call more ambiguous. Thanks.
user178884
70
votes
4 answers

Calling a Python function with *args,**kwargs and optional / default arguments

In python, I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this case, i can call func as: func(kw1=3,kw2=4,who_knows_if_this_will_be_used=7,more_kwargs=Ellipsis) I can also define a function as: def…
mgilson
  • 264,617
  • 51
  • 541
  • 636
57
votes
3 answers

Pass keyword arguments to target function in Python threading.Thread

I want to pass named arguments to the target function, while creating a Thread object. Following is the code that I have written: import threading def f(x=None, y=None): print x,y t = threading.Thread(target=f, args=(x=1,y=2,)) t.start() I…
36
votes
3 answers

python pass different **kwargs to multiple functions

From python doc and stackoverflow, I understand how to use the **kwargs in my def function. However, I have a case need two sets of **kwargs for two sub functions. Can someone show me how to separate the **kwargs properly? Here is my goal: to plot…
user3287545
  • 1,331
  • 3
  • 16
  • 17
34
votes
3 answers

How do I loop through **kwargs in Python?

In the code below, I want to read obj.subject and place it into var subject, also read obj.body and place it into body. First I want to read the kwargs variables and search for keywords within the string to replace, if none exists then move on. How…
Mo J. Mughrabi
  • 5,969
  • 14
  • 68
  • 130
34
votes
3 answers

Using an OrderedDict in **kwargs

Is it possible to pass an OrderedDict instance to a function which uses the **kwargs syntax and retain the ordering? What I'd like to do is : def I_crave_order(**kwargs): for k, v in kwargs.items(): print k, v example =…
theodox
  • 11,352
  • 3
  • 20
  • 35
34
votes
3 answers

Python: Passing parameters by name along with kwargs

In python we can do this: def myFun1(one = '1', two = '2'): ... Then we can call the function and pass the arguments by their name: myFun1(two = 'two', one = 'one') Also, we can do this: def myFun2(**kwargs): print kwargs.get('one',…
CodeArtist
  • 4,844
  • 8
  • 36
  • 59
29
votes
4 answers

Python: How to increase/reduce the fontsize of x and y tick labels?

I seem to have a problem in figuring out how to increase or decrease the fontsize of both the x and y tick labels while using matplotlib. I am aware that there is the set_xticklabels(labels, fontdict=None, minor=False, **kwargs) function, but I…
FaCoffee
  • 6,303
  • 19
  • 76
  • 148
28
votes
1 answer

empty dictionary as default value for keyword argument in python function: dictionary seems to not be initialised to {} on subsequent calls?

Here's a function. My intent is to use keyword argument defaults to make the dictionary an empty dictionary if it is not supplied. >>> def f( i, d={}, x=3 ) : ... d[i] = i*i ... x += i ... return x, d ... >>> f( 2 ) (5, {2: 4}) But…
Andrej Panjkov
  • 1,358
  • 2
  • 12
  • 17
1
2 3
54 55