Python Tutorial: How Python Variables Reference Objects

Sdílet
Vložit
  • čas přidán 13. 09. 2024
  • How Python Variables Reference Objects Python tutorial visit our website for more information at -
    learnpythontuto...
    How Python Variables Reference Objects
    How Python Variables Reference ObjectsIn the previous two Python tutorials, we discussed how Python variables reference objects which contain the value, type information and reference counter. When we say reference a variable just points to the object which is only a block of memory that contains information. When a variable points to an object that connection between the two is referred to as a reference. In this tutorial, we are going to look at how the actual reference works. Let's dive into how Python variables reference objects.
    Python Reference With One Variable and One Object
    a = 2
    In this case, the variable would point to the object. The variable is "a" and the object is "2". So, "a" points to the object that contains "2". Take a look at our diagram below.
    Python Signal Variable
    Python Reference With Two Variables Same Object
    a = 2
    b = 2
    Here we have two variables with the same value. In this case, Python would use the same object since "2" is the same for both variables. Have a look at another diagram how Python would reference the objects.
    Two Python Variables One Object
    In the above case, you can see both variables are referencing the same object even tho they are different variables.
    Variable Assigned To Another Variable
    a = 2
    b = a
    In this case, we set a = 2 and then we assign the variable "b" to the variable "a". In this case the variable "b" does not actually reference "a". "b" references "a's" object not the variable. We will use the same diagram from above since it is actually the same.
    Variable Assigned To Another Variable
    Reassigning A Variable
    a = 2
    b = 2
    The reassignment
    b = 3
    In this scenario, we have the variables "a" and "b" set to the same object then we reassign the variable "b" to a new object that contains the value of 3. How would this work? Let's take a look at another awesome diagram.
    Reassigning A Variable in Pyhton
    Reassigning a Variable That Has An Assigned Variable
    a = 2
    b = a
    The Reassignment
    a = 3
    In this scenario, we originally assign the variable "a" to the value of 2 and then we assign the variable "b" to the object of "a". Then later, on we reassign the variable "a" to a new object. In this case, "b" would still reference the object that contains the value 2 and the variable "a" would reference a new object that contains the value 3. Check out another one of my awesome drawings to better understand how this works.
    Reassigning a Variable That Has An Assigned Variable
    How To Access Python Objects Reference Counter?
    We can actually gain access to the reference counter with some coding. I will show how we can see the reference counter and how we can also see that we are using the same object when referencing the same value with different variables. The returned integer is the actual location of objects in the memory of the underlying C language.

Komentáře • 29

  • @anmjubaer
    @anmjubaer Před 2 lety +1

    That was helpful. Thanks.

  • @nipunsharma1945
    @nipunsharma1945 Před 5 lety +4

    If a= 350
    b=350
    It gives two different memory address . Can you explain why?. I'm running Python 3.7.2

  • @jachfeng6201
    @jachfeng6201 Před 5 lety +2

    Although a = 2; b = 2; both reference to the same object 2, but it's just a special case. Python has special treatment on some frequently used objects such as 0, 1, -1, etc. In most situation, for example a = 1235; b = 1235;, there will be two different object created with the same value 1235 and a, b referenced to each one separately. This can be easily check out with the id() function. The basic rule of assignment is that it never copy.

    • @Arepuplican
      @Arepuplican  Před 5 lety

      Jach Feng I am pretty sure that is mentioned and if not I apologize. There is a newer version of the video which was redone for Python 3.7 and it is mentioned there. Thanks for watching

  • @xxbighotshotxx
    @xxbighotshotxx Před 5 lety +2

    Thank you for this. This helped a lot in understanding Python's quirky way of handling object references in functions

  • @angel_machariel
    @angel_machariel Před 3 lety +1

    This is the superficial "how"I would like to know the "why". Is there a tutorial out there which shows what happens at memory level?
    For example, could it be that id() points to the starting address of the assigned data?

  • @dennisbrul9172
    @dennisbrul9172 Před 8 lety +7

    Excellent explanation! Well done! Keep up the good work! Thank you!

  • @knowledgeforever6619
    @knowledgeforever6619 Před rokem

    Good job sonny boy

  • @rammaheshwari3008
    @rammaheshwari3008 Před 6 lety +3

    Amazing Tutorial , Thanks Sir, God Bless You ❤

  • @mrthomasfritz
    @mrthomasfritz Před 6 lety +1

    would have been nice to go into link lists and double link lists.

  • @niketanjha
    @niketanjha Před 3 lety +1

    Impressive.

  • @terimsays2387
    @terimsays2387 Před 4 lety +1

    Python id == cpp *pointer ??

  • @anirudh6549
    @anirudh6549 Před 3 lety

    I love you man!!

  • @terryjordan8541
    @terryjordan8541 Před 6 lety +3

    not a very poor tutorials honestly speaking

    • @Arepuplican
      @Arepuplican  Před 6 lety

      Thanks for the comment. I believe you are saying you like the tutorial?

    • @yogeshpandey1612
      @yogeshpandey1612 Před 4 lety +1

      @@Arepuplican not a very poor reply

  • @Naz-yi9bs
    @Naz-yi9bs Před 4 lety

    The website no longer exists?

  • @elementalgroyper9863
    @elementalgroyper9863 Před 6 lety

    its didn't say that the last type was a list it sad it was a tuple?

  • @robertbencze8205
    @robertbencze8205 Před 5 lety

    wait.. then if I create a matrix.. and initialize it with 1, then it means that EVERY INDEX is referencing the SAME memory adress? wtf....

    • @mohammedjawahri5726
      @mohammedjawahri5726 Před 4 lety

      Its fucked up, I remember I was once making a 2d list by saying [ [0] *5 ] * 5 , apparently changing one element changed the entire column because they were all referring to the same memory address that's why my matrix was giving me gibberish values when modifying it

  • @Arepuplican
    @Arepuplican  Před 9 lety +3

    #Python #Programming #Coding

  • @abhishekkarn8918
    @abhishekkarn8918 Před 7 lety

    website not opening up..please help

    • @Arepuplican
      @Arepuplican  Před 7 lety

      Mastercode.online is not working?

    • @abhishekkarn8918
      @abhishekkarn8918 Před 7 lety

      you told that its a complete course!! I have completed 43 videos but still using interpreter not an editor to write full fledged codes. can u please let me know that whether I can learn to create full fledged programs after completing this series?

    • @Arepuplican
      @Arepuplican  Před 7 lety +1

      You may or may not. I do not know your knowledge level or your experience. I give you the tools to learn to python language what you do with those tools is totally up to you

    • @Arepuplican
      @Arepuplican  Před 6 lety

      How do you figure?

    • @MarkKrebs
      @MarkKrebs Před 6 lety

      Master Code Online I removed an erroneous comment of my own. Was misled by this odd behavior: a=2; b=2; then id(b)==id(a). How is this possible? When "b=2" rather than create a new object, must python search a list of all objects and see if there is already a 2 allocated somewhere in memory? That sounds like a lot of work.