#๐Ÿ”’ kawrgs.get()

63 messages ยท Page 1 of 1 (latest)

night oyster
#

I don't really understand the .get method when used with kwargs

wary needleBOT
#

@night oyster

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.

autumn radish
#

.get() is just a method for a dict

night oyster
#

Chatgpt gave me this but it confused me even more
def greet(**kwargs):
greeting = kwargs.get('greeting', 'Hello')
name = kwargs.get('name', 'World')
print(f"{greeting}, {name}!")

greet() # Uses default values
greet(greeting="Hi", name="Alice") # Overrides default values

autumn radish
#

.get(key) gets the value that matches the given key

#

!d dict.get

wary needleBOT
#

get(key, default=None)```
Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to `None`, so that this method never raises a [`KeyError`](https://docs.python.org/3/library/exceptions.html#KeyError).
autumn radish
#

So the second argument World is returned if the key name is not in the kwargs.

#

As a default value

night oyster
#

ohhh

#

That explains it

#

Ty!

autumn radish
#

kwargs is literally just a dict btw

#

Which may be confusing as well

night oyster
#

Wait really?

night oyster
autumn radish
#

!e

def greet(**kwargs):
    print(type(kwargs))

greet()  # Uses default values
greet(greeting="Hi", name="Alice")  # Overrides default values
wary needleBOT
night oyster
#

but if it's a dict

#

I need to provide a dict in the function parameters

#

Why is this not the case?

autumn radish
#

**kwargs just takes every not specified parameters

#

I forgot the name

#

The opposite is unpacking, so I guess packing

night oyster
#

ohh

autumn radish
#

You can do the opposite as well

#

!e

def add(x, y):
  return x + y

values = {"x": 5, "y": 3}
result = add(**values)
print(result)
wary needleBOT
night oyster
#

ah so you literally can give it a dict

#

that makes it much more clear

autumn radish
#

add(**values)is equivalent to add(x=5, y=3)

night oyster
#

While we are at it if you don't mind ofc

#

what about args?

autumn radish
#

just a list

night oyster
autumn radish
#

!e

def add(*args):
  print(type(args))
  total = 0
  for value in args:
    total += value
  
  return value

result = add(1, 2, 3, 4, 5)
print(result)
wary needleBOT
autumn radish
#

or tuple

#

not list

night oyster
#

google says a tuple

#

ah

autumn radish
#

There's also other stuff you can do with the *

#

!e

a, *rest, b = [1, 2, 3, 4, 5]
print(a, rest, b)
wary needleBOT
night oyster
#

oh so it took automatically all the elements in the middle

#

but made them a list not a tuple?

autumn radish
#

!e

a, *rest, b = (1, 2, 3, 4, 5)
print(a, rest, b)
wary needleBOT
autumn radish
#

Yeah, bit confusing haha

#

I kinda understand it from just using it, so I don't even know the exact rules that well

night oyster
#

Yeah xd but I think I got the general grasp

autumn radish
#

Just the patterns

full wasp
#

qq, what does that ** refers to exactly?

autumn radish
night oyster
#

I will check it out too

#

Tysm man!

#

I really appreciate your help and time

#

I think this it for now

#

Hope we meet again!

#

!close

wary needleBOT
#
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.