Closures in Python | Explained with animations

Поділитися
Вставка
  • Опубліковано 24 гру 2024

КОМЕНТАРІ • 52

  • @bhushandhuri2660
    @bhushandhuri2660 Рік тому +8

    After listening to your video at 0.75 times and doing back and forth multiple times I finally understood the closure completely.
    Thanks for the video.👍👍

  • @faramarztayyari3734
    @faramarztayyari3734 2 роки тому +21

    A weird question pops into my mind, what percentage of Python programmers really know this level of in-depth knowledge in the real world?

  • @doresearchstopwhining
    @doresearchstopwhining 10 місяців тому +1

    These videos are AMAZING. I have watched a lot of python content but IMO this is honestly the best. Not brushing over anything - very technical and the animations are top notch. Fantastic work and thank you so much. You should put up a donate link!

  • @hilmandayo
    @hilmandayo 3 роки тому +14

    This is high-quality material!!! Keep up the good work! I hope this channel will grow more and can help others.

  • @kadhiresannarayanaswamy7348
    @kadhiresannarayanaswamy7348 3 роки тому +3

    High quality, Clarified my doubts.

  • @hasinabrar3263
    @hasinabrar3263 Рік тому +1

    a very good video. I would request you to make a whole python playlist for intermediate to advanced level. You have a very unique way to explaining that make it easy to comprehend.

  • @johnaweiss
    @johnaweiss 11 місяців тому

    4:27 If `counter` no longer exists, why is it mentioned when we inspect the value of `my_inc`?

  • @MagnusAnand
    @MagnusAnand 3 роки тому +7

    Nice video! Although a little bit too fast. Thanks!

  • @ulysses_grant
    @ulysses_grant 3 роки тому +3

    This is great. Absolutely great.

  • @dr.strangelove5622
    @dr.strangelove5622 2 роки тому +3

    This is an amazing video!! Excellent explanation and animations!! I have subscirbed to you!! By the way, which software do you use for these animations?

    • @sreekanthpr
      @sreekanthpr  2 роки тому +1

      I used github.com/3b1b/manim

  • @priyankagupta119
    @priyankagupta119 2 роки тому +1

    I like your way of teaching with visuals. Really helpful!!

  • @charprototypia7793
    @charprototypia7793 Рік тому

    You can do this:
    def counter(num):
    def increment(inc=1):
    nonlocal num
    num += inc
    return num
    return increment
    counter10 = counter(10)
    closureList = [counter10(3) for a in range(0, 5)]
    print(closureList)
    to output this:
    [13, 16, 19, 22, 25]
    However, if you don't return num in the inner function, the output (and the list) will be:
    [None, None, None, None, None]
    because the inner func is returning nothing, i.e, is returning "None" in each iteration

  • @tomerlevi2640
    @tomerlevi2640 Рік тому

    great explanation good animations, keep on!

  • @catchalok3
    @catchalok3 3 роки тому +4

    Amazing.. Sreekant, can you please tune voice over a little bit? I am using 0.75 but still sometimes few words are very fast. And more videos please :). Keep up the good work.

  • @skirfan9908
    @skirfan9908 Рік тому

    awsme!! hope u are doing more such videos ..

  • @imtiaz10132five
    @imtiaz10132five 3 роки тому +3

    What are the use cases of closures in python?

    • @sreekanthpr
      @sreekanthpr  3 роки тому +5

      It's the base for decorators. For example, you could implement a basic equivalent of '@functools.lru_cache' using a dictionary as the free variable.

    • @brahmabull4004
      @brahmabull4004 3 роки тому

      @@sreekanthpr a beginner (which I assume OP is based on his question) has no idea what you just said in your example.

  • @johnaweiss
    @johnaweiss 11 місяців тому

    5:08 to me, an object is a thing that has methods, like
    counter.increment()
    To me, it's counter-intuitive to say
    my_inc.increment()

  • @sauravtarun8495
    @sauravtarun8495 2 роки тому

    would love to connect and see more videos !

  • @DrIT-qv1ek
    @DrIT-qv1ek 5 місяців тому

    Exceptional!

  • @LamNguyen-nm1fq
    @LamNguyen-nm1fq 2 роки тому

    This is great, bro. I've followed your channel.

  • @johnaweiss
    @johnaweiss 11 місяців тому

    Great video, deep info. I had to watch at 0.75 speed cuz so deep.

  • @Tobi_Jones
    @Tobi_Jones 2 роки тому

    good video, the animations are helpful

  • @Han-ve8uh
    @Han-ve8uh 3 роки тому

    At 3:50 the arrow pointed to 5 directly, but at 6:45 it pointed to a memory address which in turn points at 5.
    Is this an actually difference or because you didn't want to complicate things and animated the easy way at 3:50?
    3:50 example was using start+=step and 6:45 example (cell object) was using start=start+step, is this what is causing the difference?
    I ran both examples and observed their co_freevars, closure, co_varnames and they look exactly the same. This makes me think the arrow at 3:50 should also be pointing at a memory address like 6:45 and start+=step vs start=start+step has no difference in this lesson.
    If this is right, why complicate things using 2 different syntax, if wrong, what differences does += vs + cause in terms of closures?

    • @sreekanthpr
      @sreekanthpr  3 роки тому

      At all times, Python stores pointers to actual objects and Python takes care of automatic dereferencing (explained in another video ua-cam.com/video/0Om2gYU6clE/v-deo.html, also mentioned at 1:33 in the current video). At 6:45, instead of this one hop, there are two hops. Sometimes I just directly show the value because it's easy, requires less space on the screen, and that part is not what I will be explaining at that point in time.
      With respect to '+=', there is a difference in how augmented arithmetic operators are handled(explained here ua-cam.com/video/0Om2gYU6clE/v-deo.html). But for immutable objects, end result will be same. I just wanted to make it explicit.

    • @Han-ve8uh
      @Han-ve8uh 3 роки тому

      @@sreekanthpr ​ @Sreekanth Thanks for your reply, i have watched the other videos too, really grateful that you make such clear animations to help learners.
      I understand the difference between += and + from that other video, but when it comes to this video, I still wish to reiterate my question.
      1. Why is 3:50 1 hop and 6:45 2 hops?
      2. Is the difference in 1. caused by writing start+=step vs start=start+step?
      3. Is start+=step vs start=start+step used to demonstrate some other closure related concept in this video if it doesn't cause 1.?

    • @sreekanthpr
      @sreekanthpr  3 роки тому

      Thanks!
      There is no difference in start+=step vs start=start+step in closure. I just wanted to emphasize the right-hand side is evaluated first and it gets assigned back to the 'start' variable, that's why I chose to write it explicitly.
      At 3:50 also there are 2 hops (the cell object exists). But the viewer is assumed to be not aware of the existence of cell objects at that point in time. At 06:32 is where I introduce this internal implementation detail.

    • @nirajraut9408
      @nirajraut9408 3 роки тому

      @@sreekanthpr Hey, I got everything that you said except for the definition of "closure". It's unclear whether "inner" is a closure. If the enclosed function _(enclosed inside another function, in this case, "inner")_ contains free variables, only then it's a closure, right? I think there are some technical details that I am not able to manacle.
      _PS: This is high-quality, quintessential content. I am looking forward to "Classes and Objects", "Decorators", "Metaclasses", etc. Also, you should start making videos on Data Structures and Algorithms with Python. I was always interested in the implementation details of CPython. May you achieve 10 million subscribers! I wish you the best of luck!_ :-)

    • @sreekanthpr
      @sreekanthpr  3 роки тому +1

      @@nirajraut9408 Yes, the "inner" function is a closure if it it references any variable in its nonlocal scope. Those variables are called "free variables". So, closure is "function + free variables" (or "function + environment")
      Next video might be on Python's import system covering things like module objects. After that I'm thinking of starting series on data structures.

  • @toan3931
    @toan3931 2 роки тому

    awesome explanation, really helpful. I look forward upcoming video!!!

  • @talalkalai8748
    @talalkalai8748 2 роки тому

    Can u make a video bout python's generators/iterators memory layout

  • @krushna7467
    @krushna7467 2 роки тому +1

    awesome explanation❤‍🔥 thanks a ton for creating such quality content in best way to make difficult topics easy to understand with deepest explanation 💯

  • @kewtomrao
    @kewtomrao 2 роки тому

    Amazing stuff!!!

  • @RahulSharma-lw2ss
    @RahulSharma-lw2ss Рік тому

    excellent

  • @shubhamtank2278
    @shubhamtank2278 3 роки тому

    best explaination

  • @ThienNguyen-lh3bj
    @ThienNguyen-lh3bj 3 роки тому +3

    Love this :3

  • @M.I.S
    @M.I.S 2 роки тому

    thank YOU!

  • @wrlccywrlfh
    @wrlccywrlfh 2 роки тому +4

    too fast bro

    • @sreekanthpr
      @sreekanthpr  2 роки тому +2

      I'm adding higher pauses between sentences in newer videos.

  • @ReubenSilveira
    @ReubenSilveira Місяць тому

    I heard the word 'function' too many times and now I've forgotten what it means.

  • @viveknegi1
    @viveknegi1 3 роки тому +1

    Dude , When are you going to upload new video ?

  • @yt-1161
    @yt-1161 2 роки тому

    I needed for this 8 min video around 40 min to watch. I still don't get everything

    • @MM_Legacy
      @MM_Legacy 2 роки тому +1

      I had to pause it every few seconds. It's just too quick and full of specialized terminology. Using pause by lector would be beneficial as it would give us time to assimilate the knowledge. I liked the drawings though. :)

  • @rahulmistry5381
    @rahulmistry5381 7 місяців тому

    Video is nice but speak little slower.

  • @tejas4054
    @tejas4054 9 місяців тому

    Hindi me channel shuru kar ,,,nhi to nhi samzega kuch