Python Function Argument Unpacking Tutorial (* and ** Operators)

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024

ความคิดเห็น • 57

  • @donatellodonini3147
    @donatellodonini3147 3 ปีที่แล้ว +1

    I was struggling on a solution with an issue with my program for days and than i saw this video and yiu solved my problem, thank you!!!!

  • @kurtstephens9409
    @kurtstephens9409 4 ปีที่แล้ว +9

    I commonly use dictionary unpacking for database and API connections that take a number of keyword arguments. Simply store the configuration in a dict and pass it to the function:
    db_config = {
    'user': 'Joe',
    'password': 'secret',
    'host': 'localhost',
    'db': 'production'
    }
    connection = connect(**db_config)

    • @tech-learner4555
      @tech-learner4555 ปีที่แล้ว

      You can directly pass the dict and still performs the same fn right?

    • @jamestillman5247
      @jamestillman5247 ปีที่แล้ว

      simple and powerful I like it

  • @rossli8621
    @rossli8621 3 ปีที่แล้ว +1

    Thanks bro, you saved my project.

  • @Xaminn
    @Xaminn 4 ปีที่แล้ว +6

    This is incredible. This was exactly what I needed. Thanks a lot!

  • @gedtoon6451
    @gedtoon6451 18 วันที่ผ่านมา

    The * operator can be used to unpack an iterable into separate variables. The ** can be used to unpack a dictionary. But in the case of *args and **kwargs used as function parameters, they are packing and not unpacking. *args takes several positional arguments and packs them into a tuple. **kwargs takes several named parameters and packs them into a dictionary.

  • @pawjast
    @pawjast ปีที่แล้ว

    Good tutorial. Thanks for sharing.

  • @777adrian
    @777adrian 7 ปีที่แล้ว +1

    unpacking on named arguments is awesome

  • @382946rthu
    @382946rthu 7 ปีที่แล้ว +28

    A cool thing to note is a * on a dict will return the keys.

    • @382946rthu
      @382946rthu 7 ปีที่แล้ว +7

      Haha never mind :)

    • @tech-learner4555
      @tech-learner4555 ปีที่แล้ว

      It doesn't

    • @tech-learner4555
      @tech-learner4555 ปีที่แล้ว

      d = {
      "K1" : "v1",
      "K2" : "v2"
      }
      def myfun(*d):
      print(d)
      myfun(d)
      And it prints the key value pairs not only keys

  • @spelillo
    @spelillo 3 ปีที่แล้ว +2

    Hi there, regarding 8:05 comment I have just discovered:
    If you want to use dictionary values but keys don't match our function arguments you can just write
    print_vector(*(dict_vec).values())

  • @jackaubrey8614
    @jackaubrey8614 3 ปีที่แล้ว

    Very, VERY good! Thank you.

  • @maxvinella941
    @maxvinella941 6 ปีที่แล้ว

    I Purchased book on Kindle...enjoying it!!

    • @realpython
      @realpython  6 ปีที่แล้ว +1

      That's great! Glad to hear it.

  • @alexismandelias
    @alexismandelias 3 ปีที่แล้ว

    Best explanation on the internet hands down

  • @kasamajay
    @kasamajay 3 ปีที่แล้ว

    Hi, can you please let me know your python development environment. I can see that this video is made on Mac. But i'm not sure what python console are you using. I'm surprised to see that its autocomplete, auto suggestions, and intellisense all enabled. is it plain python3 (command) or is it pycharm ide's python3 console or jupyter notebook online or jupyter notebook offline?
    Can you please let us know this
    Thanks and Regards
    Ajay Kumar Kasam

  • @hamzah6830
    @hamzah6830 5 ปีที่แล้ว

    thank you bro, now i understund how can i work with argument unpacking for list, tuples, dictionnary thank's again

  • @horborzt
    @horborzt 5 ปีที่แล้ว

    Thanks for the explanation!

  • @الزينولدالمختار
    @الزينولدالمختار 5 ปีที่แล้ว

    PLEASE ANSWER:
    does this means that the value returned represent a vector?, like when it is like this [1,0,1] it means that it is a list.

    • @machinelearningbot2037
      @machinelearningbot2037 5 ปีที่แล้ว

      This was added as part of the definition in the print_vec() function. Nothing to do with whether it represents a vector.

  • @AnhNguyen-vu7mc
    @AnhNguyen-vu7mc 7 ปีที่แล้ว +2

    how do you make your terminal show suggestions for python?

    • @banjohead66
      @banjohead66 6 ปีที่แล้ว +1

      He's using Sublime Text with several plug-ins -- he has a separate book/course about how to set up ST specifically for Python development.

  • @zemalex89
    @zemalex89 7 ปีที่แล้ว +1

    how do you solve the problem if you don't know the names of the dictionary keys?

  • @nextgodlevel
    @nextgodlevel 6 ปีที่แล้ว

    Sir, I want to use object name in class before making the object that means inside the class?????????

  • @kravikiran2054
    @kravikiran2054 4 ปีที่แล้ว

    We can define function as print_vector(list1=[]) and same way for tuple
    How is the difference

  • @liyuliang7054
    @liyuliang7054 7 ปีที่แล้ว

    Great video, Thanks Dan!

  • @vishnugajulapalli5305
    @vishnugajulapalli5305 3 ปีที่แล้ว

    Can someone explain to me, how to check whether a given time in "hh:mm" format falls in between a given range.
    Say, given time is 10:30 A.M IST and my range is between 10:00 A.M and 11:00 A.M. So given time falls in the range.
    Is there any package in python to do this in the easiest way?
    Would be happy if anyone can help with this :)

  • @ms77grz
    @ms77grz 5 ปีที่แล้ว

    and I just wrote my own reader to check the star :-)
    def text_reader(*text):
    print(*text, sep='')
    with open('./test.txt') as f:
    reader = f.read()
    text_reader(*reader)

  • @sleepydunk
    @sleepydunk 5 ปีที่แล้ว

    thanks bro

  • @danielRvallejo
    @danielRvallejo 7 ปีที่แล้ว

    what are you using to code, console?

  • @cautioni
    @cautioni 6 ปีที่แล้ว +1

    can you explain *_ please?

    • @realpython
      @realpython  6 ปีที่แล้ว

      Thanks for the suggestion!

    • @SohailKhan-kh2ch
      @SohailKhan-kh2ch 6 ปีที่แล้ว

      Python Training by Dan Bader Can you explain me how did you made this program in another video or any other way

  • @arkasaha4412
    @arkasaha4412 5 ปีที่แล้ว

    Why exactly did the keys occur in random order?

    • @James-bv4nu
      @James-bv4nu 3 ปีที่แล้ว

      Because LIST and TUPLE are containers where the elements are ordered;
      the position of the elements has meaning (the positions are meta data);
      and you can access the elements by position (index).
      SET and DICTIONARY are containers where the elements are not ordered;
      the position of the elements has no meaning, and you cannot access the elements by position.

  • @jimmycheong7970
    @jimmycheong7970 7 ปีที่แล้ว

    Thank you so much! This was great

    • @realpython
      @realpython  7 ปีที่แล้ว +1

      You're welcome! I'm happy that you liked it!

  • @fet1612
    @fet1612 5 ปีที่แล้ว

    3:06 Realy Python:
    Dan Bader:
    "... because function argument unpacking can do something really similar to what we'd been doing here manually where we're unpacking this tuple manually.
    There is actually a shorthand to do that in Python."

  • @fet1612
    @fet1612 5 ปีที่แล้ว

    3:03 Real Python:
    Dan Bader:
    "... and this is where the function argument unpacking could become really helpful."

  • @fet1612
    @fet1612 5 ปีที่แล้ว

    3:25
    Dan Bader:
    "So what we can do here is we can call the print_vector function again. I can use the * operator, the unpacking operator, and just pass my tuple vector to that print_vector function."

  • @vithushanlogeswaran8422
    @vithushanlogeswaran8422 3 ปีที่แล้ว

    to me for this(**) it is giving error

  • @fet1612
    @fet1612 5 ปีที่แล้ว

    2:58
    Dan Bader:
    "If I want to print this vector, it gets kind of awkward. I am sure I could write a different function that would take a tuple vector directly, but what if my vector is going to be represented as a dictionary, or something else, it gets really awkward ...

  • @UltramaticOrange
    @UltramaticOrange 7 ปีที่แล้ว

    ... just realized that I've been doing things like my_func(*some_dict.values()) and how someday that's going to bite me in the ass ... whoops...

  • @slimyelow
    @slimyelow 5 ปีที่แล้ว

    who here doesn't love to lay pipe...

  • @nextgodlevel
    @nextgodlevel 6 ปีที่แล้ว

    I buy python programming introduction to computer science by john zelle

    • @realpython
      @realpython  6 ปีที่แล้ว +1

      Thanks for sharing!

  • @echo2net
    @echo2net 2 ปีที่แล้ว

    Are you Doctor Strange?

  • @James-bv4nu
    @James-bv4nu 3 ปีที่แล้ว

    If you're listening to this, and you don't cringe, then you're not a C programmer.