Python args kwargs

Discussion in 'Tech Discussion' started by Ai chan, Feb 8, 2019.

  1. Ai chan

    Ai chan Queen of Yuri, Devourer of Traps, Thrusted Witch

    Joined:
    Nov 7, 2015
    Messages:
    11,278
    Likes Received:
    24,346
    Reading List:
    Link
    So Ai-chan was looking at documentations of python and Ai-chan found these words args kwargs a lot.

    Those who are good with python, can you tell Ai-chan what these are and how to use them? Ai-chan googled but still can't understand. Sounds too much like chocobo gibberish. Like can't people just put stuff like achievement.register(name, **kwargs) into achievement.register(name, textvariableorwhatever)? Why put something so strange?
     
    gangbuntu likes this.
  2. elengee

    elengee Daoist Ninefaps

    Joined:
    Mar 15, 2016
    Messages:
    13,488
    Likes Received:
    25,896
    Reading List:
    Link
    So you're only good with one-eyed snakes and manacondas. :blobwoah:
     
  3. WinByDying

    WinByDying I can count to four

    Joined:
    Jul 18, 2017
    Messages:
    922
    Likes Received:
    932
    Reading List:
    Link
    Say you have a function:

    def foo(animal,owner,leash,*args,**kwargs):
    pass

    (indents don't seem to work on NUF)

    Then if you call foo(cat,John,None,"lazy cat",1,name="Garfield"), the normal (named?) arguments (animal, owner, leash) get values cat, John and None respectively. The args are "lazy cat" and 1, put into the args list in that order, and name="Garfield" is a kwarg (key word argument?).

    So you get a list called args and a dict called kwargs. I think. It's been some time. Edit: the names can be changed if you want, just keep the order(?) and the asterisks the same.

    I think the Python devs took that over from C. (A few similarities with argc/argv for main, but no real C/C++ equivalent.) It's a way to sort of ... pass through generic arguments to a function?
     
    Last edited: Feb 14, 2019
    Ruyue, gangbuntu, Ai chan and 9 others like this.
  4. Deleted member 155674

    Deleted member 155674 Guest

    Reading List:
    Link
    Nice explanation (y)
     
  5. RyouDou

    RyouDou Active Member

    Joined:
    Dec 17, 2016
    Messages:
    19
    Likes Received:
    22
    Reading List:
    Link
    They are used to pass variable number of arguments or keyword arguments to functions when you don't know how many arguments there will be at the time the function is defined.

    For example, you could put args in a tuple and kwargs in a dict and prepend the variable names with *s in function call.

    my_args = (1, 2, 3)
    my_kwargs = {"arg0": 6, "arg1": 4}
    my_func(*my_args, **my_kwargs)

    Edit: I think this is a good post about this: https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
     
    Last edited: Feb 8, 2019
  6. Djoty

    Djoty Well-Known Member

    Joined:
    Dec 19, 2016
    Messages:
    300
    Likes Received:
    140
    Reading List:
    Link
    Missed my programming lecture today, now am being reminded of it
     
  7. Jiggy

    Jiggy I am JiggyliFAP~ the not fat anymore guy.

    Joined:
    Jun 4, 2016
    Messages:
    1,710
    Likes Received:
    2,021
    Reading List:
    Link
    Oh shit. I thought you were looking on Python documentaries. Turns out it something about programming, lol.
     
    Flyingfrogslash likes this.
  8. TerraEarth

    TerraEarth Well-Known Member

    Joined:
    Apr 13, 2016
    Messages:
    414
    Likes Received:
    256
    Reading List:
    Link
    You can usually find this stuff right away from a google search, not that it matters. It would be faster for you to search them this way since I'm sure you'll have many more questions to come.
     
  9. Deleted member 37987

    Deleted member 37987 Guest

    Reading List:
    Link
    That's a good remark, it's not very interesting figuring out why they don't just do a Google search though.
     
  10. Nanashy

    Nanashy Scarybun of Horror

    Joined:
    Mar 13, 2016
    Messages:
    710
    Likes Received:
    1,502
    Reading List:
    Link
    WinByDying's explanation was the easiest and most understandable i have seen. Usually in the programming questions forums they do not give such foolproof answers, or at least rarely do so.
     
    Lokumi likes this.
  11. TerraEarth

    TerraEarth Well-Known Member

    Joined:
    Apr 13, 2016
    Messages:
    414
    Likes Received:
    256
    Reading List:
    Link
    Several reasons why they might have done such a thing, takeaway however is that it's actually much easier (for them) to do a quick google.

    The analogy is like this:
    You're sitting in a chair and there's a bowl of chips in front of you. You want chips, all you have to do is reach out and take them! But instead, you get up off your chair, search around the house, find somebody and ask them if they could come to the living room and grab some chips for you. Much more effort than it's worth, most of the time - unless you're trying to socialize.
     
    Deleted member 37987 likes this.
  12. losthound

    losthound Active Member

    Joined:
    Dec 27, 2017
    Messages:
    13
    Likes Received:
    4
    Reading List:
    Link
    WinByDying has a good explanation for what they do.
    If you are asking why take a look at str.format() it allows users to put arbitrary variables into the function to produce meaningful output. In addition it serves as way to do the opposite of *list or **dict (expansion) for functions and gather the items. Also stackoverflow is your friend for questions like this.
     
  13. Jeebus

    Jeebus Well-Known Member

    Joined:
    Jun 20, 2017
    Messages:
    904
    Likes Received:
    780
    Reading List:
    Link
  14. Flyingfrogslash

    Flyingfrogslash Well-Known Member

    Joined:
    Feb 11, 2016
    Messages:
    262
    Likes Received:
    263
    Reading List:
    Link
    I spent the last few minutes just staring and reading the posts trying to figure out what they were on about with programming language before my sleep deprived brain finally remembered that Python is not only a type of snake but a programming language. I feel like a tired moron.
     
  15. raitei

    raitei ⟪Procyon lotor paronomasiaabsentii⟫

    Joined:
    Sep 8, 2017
    Messages:
    678
    Likes Received:
    989
    Reading List:
    Link
    What's wrong with one-eyed snakes?
    [​IMG]
     
    elengee likes this.
  16. Deleted member 37987

    Deleted member 37987 Guest

    Reading List:
    Link
    You can expand on this in more negative ways.
     
  17. Arcturus

    Arcturus Cat, Hidden Sith Lord

    Joined:
    Jan 1, 2016
    Messages:
    9,273
    Likes Received:
    17,814
    Reading List:
    Link
    I mean if you actually read what @Ai chan said in her OP, you'd realize she did try to google it and she couldn't understand their explanations.
     
    Nanashy likes this.
  18. TerraEarth

    TerraEarth Well-Known Member

    Joined:
    Apr 13, 2016
    Messages:
    414
    Likes Received:
    256
    Reading List:
    Link
    Oh I read it. And I don't believe her. Not that I think she's doing anything wrong, I was just giving some advice as I believe that unless you're dealing with something truly abstract, usually at a high level (which she obviously is not at, given the context), you can find many well-explained, documented and exampled responses online.
     
    Deleted member 37987 likes this.
  19. Arcturus

    Arcturus Cat, Hidden Sith Lord

    Joined:
    Jan 1, 2016
    Messages:
    9,273
    Likes Received:
    17,814
    Reading List:
    Link
    No offense, but frequently for the novice programmer, which I'm pretty sure she is, online help is incomprehensible. They explain things as if they assume you are very much familiar with the language in question and with programming. And while I'm sure after reading a bunch of articles and slowly piecing it together, she could figure it out, she has no desire to be a programmer in and of itself. She's not trying to figure out the language, but how to simply and practically use it. And generally online help isn't optimized to do that.
     
    Last edited: Feb 9, 2019
    Nanashy and LaDyViL like this.
  20. TerraEarth

    TerraEarth Well-Known Member

    Joined:
    Apr 13, 2016
    Messages:
    414
    Likes Received:
    256
    Reading List:
    Link
    This is where we disagree then. Like I stated earlier, my comment was a suggestion made with her best interest in mind, not a decree from the heavens - and yes, I made the suggestion with the assumption that she's a beginner. She can do w/e she wants.
     
    Deleted member 37987 likes this.