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

1010 questions
1542
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,901
  • 10
  • 42
  • 48
846
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 classified as…
Federer
  • 33,677
  • 39
  • 93
  • 121
595
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
  • 27,892
  • 14
  • 72
  • 91
458
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
  • 4,813
  • 2
  • 16
  • 8
166
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,634
  • 8
  • 30
  • 34
121
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
  • 4,166
  • 4
  • 26
  • 27
84
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
78
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
  • 300,191
  • 65
  • 633
  • 696
78
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…
49
votes
4 answers

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
  • 7,609
  • 28
  • 99
  • 174
46
votes
4 answers

Python: Difference between kwargs.pop() and kwargs.get()

I have seen both ways but I do not understand what the difference is and what I should use as "best practice": def custom_function(**kwargs): foo = kwargs.pop('foo') bar = kwargs.pop('bar') ... def custom_function2(**kwargs): foo =…
Aliquis
  • 2,091
  • 4
  • 21
  • 40
46
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,911
  • 5
  • 20
  • 19
45
votes
2 answers

How to make "keyword-only" fields with dataclasses?

Since 3.0 there is support to make an argument keyword only: class S3Obj: def __init__(self, bucket, key, *, storage_class='Standard'): self.bucket = bucket self.key = key self.storage_class = storage_class How to get…
wim
  • 338,267
  • 99
  • 616
  • 750
42
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
  • 6,747
  • 16
  • 85
  • 143
40
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
  • 5,534
  • 8
  • 40
  • 65
1
2 3
67 68