args and kwargs

suggest change

Remarks

There a few things to note:

  1. The names args and kwargs are used by convention, they are not a part of the language specification. Thus, these are equivalent:
    def func(*args, **kwargs):
        print(args)
        print(kwargs)
    #
    def func(*a, **b):
        print(a)
        print(b)
  2. You may not have more than one args or more than one kwargs parameters (however they are not required)
    def func(*args1, *args2):
    #   File "<stdin>", line 1
    #     def test(*args1, *args2):
    #                      ^
    # SyntaxError: invalid syntax
    #
    def test(**kwargs1, **kwargs2):
    #   File "<stdin>", line 1
    #     def test(**kwargs1, **kwargs2):
    #                       ^
    # SyntaxError: invalid syntax
  3. If any positional argument follow *args, they are keyword-only arguments that can only be passed by name. A single star may be used instead of *args to force values to be keyword arguments without providing a variadic parameter list. Keyword-only parameter lists are only available in Python 3.
    def func(a, b, *args, x, y):
        print(a, b, args, x, y)
    
    func(1, 2, 3, 4, x=5, y=6)
    #>>> 1, 2, (3, 4), 5, 6
    #
    def func(a, b, *, x, y):
        print(a, b, x, y)
    
    func(1, 2, x=5, y=6)
    #>>> 1, 2, 5, 6
  4. **kwargs must come last in the parameter list.
    def test(**kwargs, *args):
    #   File "<stdin>", line 1
    #     def test(**kwargs, *args):
    #                      ^
    # SyntaxError: invalid syntax

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


args and kwargs:
* args and kwargs

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
83 args and kwargs
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve