#🔒 Confused about formal parameters

32 messages · Page 1 of 1 (latest)

placid trail
#

In the python documentation, tutorial section(https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions), an example shows use of formal parameters. The function cheesecake is called without using keyword arguments but I want to know how to call the function using keyword arguments like -
cheesecake(kind = "Limburger", arguments = (tuple), keywords = {dictionary})

How should I do this?

primal muskBOT
#

@placid trail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hearty hatch
#

Oh, cheeseshop.

placid trail
#

yeah, sorry cheeseshop

hearty hatch
#

Their example has:

  • one positional parameter named kind
  • accepts any number of other positional parameters
  • accepts any number of keyword parameters
#

So if you call cheeseshop("Jarlsberg", "a", 1, 2, x=3, y=4, j="fred") ....

#

Then the function receives:

  • kind="Jarlsberg"
  • arguments=("a", 1, 2)
  • keywords={"x":3, "y":4, "j":"fred"}
placid trail
#

any way to mention these during function call, like -

#

cheeseshop(kind = "Jarlsberg", arguments = ("a", 1, 2), keywords = {"x": 3, "y": 4, "j": "fred"})

#

?

hearty hatch
#

No.

hollow spear
#

the * and ** indicate that they are special parameters and are not referred to by name

hearty hatch
#

The *args and **keywords mean "any positional parameters we have not named" and "any keyword parameters we have not named"

hollow spear
#

it simply means "allow this function to take an arbitrary amount of arguments"

hearty hatch
#

So you can't name aerguments and keywords at the calling end. You just supply position or keyword parameters.

placid trail
#

alright, understood. Thanks!👍

hollow spear
#

if you knew the name of the parameter, you wouldn't be using **

hearty hatch
#

It's handy for wrappers, where you support some arguments specially but want to pass everything else on to some other function.

hollow spear
#

do you have a scenario that you think you might want it?

hearty hatch
#

... or where you just accept any number of arguments, like the print() function does.

placid trail
hollow spear
placid trail
# hollow spear yes

Sorry, cannot think of any yet! If you have examples of such scenarios, please do tell.

hollow spear
hearty hatch
#

Well, you can't name them so there aren't any 🙂

placid trail
#

I see

hearty hatch
#

I think the OP just wanted clarity on what the docs were describing.

placid trail
#

Thanks for clarifying👍 🫰

#

!close

primal muskBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.