#๐ kawrgs.get()
63 messages ยท Page 1 of 1 (latest)
@night oyster
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.
.get() is just a method for a dict
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
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).
So the second argument World is returned if the key name is not in the kwargs.
As a default value
Wait really?
It is ๐
!e
def greet(**kwargs):
print(type(kwargs))
greet() # Uses default values
greet(greeting="Hi", name="Alice") # Overrides default values
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | <class 'dict'>
002 | <class 'dict'>
but if it's a dict
I need to provide a dict in the function parameters
Why is this not the case?
**kwargs just takes every not specified parameters
I forgot the name
The opposite is unpacking, so I guess packing
ohh
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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
8
add(**values)is equivalent to add(x=5, y=3)
just a list
a list or a tuple?
!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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | <class 'tuple'>
002 | 5
There's also other stuff you can do with the *
!e
a, *rest, b = [1, 2, 3, 4, 5]
print(a, rest, b)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
1 [2, 3, 4] 5
oh so it took automatically all the elements in the middle
but made them a list not a tuple?
!e
a, *rest, b = (1, 2, 3, 4, 5)
print(a, rest, b)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
1 [2, 3, 4] 5
Yeah, bit confusing haha
I kinda understand it from just using it, so I don't even know the exact rules that well
Yeah xd but I think I got the general grasp
Just the patterns
ah
qq, what does that ** refers to exactly?
This probably explains it better than I can
https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters
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
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.