GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing python-course.eu — 26. Parameters And Arguments | Python Tutorial | python-course.eu 26. Parameters And ArgumentsBy Bernd Klein. Last modified: 10 Nov 2023. On this page ➤ IntroductionAt first the definition and usage of Python parameters and arguments seems to be simple, but there exists a multitude of intricate details that can be perplexing. So that you don't get lost in the "parameter wood", we provide this introduction. Let's start with a simple question: If you look at the following code, is everything clear to you? If so, you don't have to continue reading this chapter of our Python tutorial. If not, you should definitely continue reading. def all_together_now(a, /, b, c=11, d=12, *args, kw_only1=42, kw_only2=84, **kwargs): return f'{a=}, {b=}, {c=}, {d=}, {args=}, {kw_only1=}, {kw_only2=}, {kwargs=}' Live Python training See our Python training courses Required ParametersLet's look first at a function with one parameter which can or better has be called with a positional or keyword argument: def positional_example(x): return x + 42 We can call this function with a numerical value like this: positional_example(1) OUTPUT:43 We can also call it with a keyword argument, i.e. by assigning the object explicitly to the parameter name: positional_example(x=1) OUTPUT:43 Positional Only ParametersPositional-only parameters have no externally-usable name. This implies that you cannot assign an object to the parameter's name when making a function call. When a function accepting positional-only parameters is called, positional arguments are mapped to these parameters based solely on their order. Positional-only Parameters are a way to prevent using arguments as keyword arguments. We demonstrate this in the following example. All the parameters (in our example only the parameter x) before the '/' are defined as positional only arguments. The parameters following the '/' can be used both positionally and as keyword arguments: def positional_only_example(x, /, y, z): return x + 42 positional_only_example(1, 4, 6) OUTPUT:43 As we mentioned before, we can use positional_only_example(1, z=6, y=4) OUTPUT:43 If you try to use x as a keyword argument
m you will get the exception
Optional Positional Only ParametersThis is the same as before, but the function can also be called without giving an argument: def optional_positional_only_example(x=42, /): return x + 42 print(optional_positional_only_example(1)) # or without an argument: print(optional_positional_only_example()) OUTPUT:43 84 Keyword-only ParameterSo, we have seen parameters which can only be used in a call as positional but not as keyword parameters. What about parameters that must be utilized as keyword arguments and cannot be employed as positional arguments? def keyword_only_parameters(*, key): return key keyword_only_parameters(key=99) OUTPUT:99 But you cannot call Live Python training See our Python training courses Optional Keyword-only ParameterLike before, but if the keyword is not given in the function call the default value will be uses: def keyword_only_parameters(*, key=42): return key print(keyword_only_parameters(key=99)) print(keyword_only_parameters()) OUTPUT:99 42 Keyword-only Parameters after *argsIt's easy to imagine a function that accepts a variable number of arguments along with one or more 'options' provided as keyword arguments. There is a way to add these options after the arbitrary list of optional positional parameters. def keyword_only_args(x, *args, a, b): return x, args, a, b print(keyword_only_args("pos", "arg1", "arg2", "arg3", a=3, b=4)) OUTPUT:('pos', ('arg1', 'arg2', 'arg3'), 3, 4)
Live Python training See our Python training courses All Together Nowdef all_together_now(a, /, b, c=11, d=12, *args, kw_only1=42, kw_only2=84, **kwargs): return f'{a=}, {b=}, {c=}, {d=}, {args=}, {kw_only1=}, {kw_only2=}, {kwargs=}' all_together_now(2, 4) OUTPUT:'a=2, b=4, c=11, d=12, args=(), kw_only1=42, kw_only2=84, kwargs={}'
all_together_now(2, name='Guido', age=42, kw_only1=99, b=1001) OUTPUT:"a=2, b=1001, c=11, d=12, args=(), kw_only1=99, kw_only2=84, kwargs={'name': 'Guido', 'age': 42}"
Keyword Arguments vs. Optional Parameters"Keyword argument" is like the term "argument" implies related to a function call and not in the function definition. Quite often people confuse optional parameters in a function definition with keyword parameters. def foobar1(optional_parameter1=42, optional_parameter2=22): return optional_parameter1 + optional_parameter2 def foobar2(positional_parameter1, positional_parameter2): return positional_parameter1 + positional_parameter2 Both functions can be called with positional or keyword parameters, as you can see in the following Python function calls: print(foobar1(1, 99)) print(foobar1(1, optional_parameter2=99)) print(foobar1(optional_parameter1=1, optional_parameter2=99)) OUTPUT:100 100 100 Now, the same way of calling with foobar2: print(foobar2(1, 99)) print(foobar2(1, positional_parameter2=99)) print(foobar2(positional_parameter1=1, positional_parameter2=99)) OUTPUT:100 100 100 The distinction between these two functions is rooted in the flexibility of their argument handling. print(foobar1()) print(foobar1(1)) print(foobar1(optional_parameter2=6)) print(foobar1(1, 99)) OUTPUT:64 23 48 100 |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |