Pythons self parameter

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • Discusses Python self parameter

Komentáře • 43

  • @crossfarm4146
    @crossfarm4146 Před 4 lety +13

    On behalf of all Learning Python Programmers, We thank you for giving this freely accessible , clear and concise information on the subject. If I'm ever able to make money from programming (which I hope one day) I would like to help support you on patreon or something.

  • @S24W2
    @S24W2 Před 7 lety +14

    fantastic explanation, it's mad the way others can't explain this concept without confusing you more, but this is a great explanation, thank you

  • @laturchasanket6901
    @laturchasanket6901 Před 4 lety +4

    Learnt python from many sources but this concept of self, couldn't understand this better. Thanks for this amazing video, cleared my most of the doubts.

  • @FitLife6767
    @FitLife6767 Před 7 lety +4

    I'd like to thank you sir for being so generous in supplying us all with such a great presentation...You are blessing many people with this attitude ...Once again thank you from Brazil...

  • @bikkinarohith1006
    @bikkinarohith1006 Před 4 lety +4

    extraordinary explanation...deep into concept and exactly in a way that people must understand the concept!

  • @greenparksandblueskies9099

    Again, clearly explained as well as any boot camp one would pay for.

  • @matematleta492
    @matematleta492 Před měsícem +1

    Best explanation of this topic anywhere. Thank you.

  • @bensondavies7716
    @bensondavies7716 Před 6 lety +2

    You have changed my life. Went from doing to understanding which is a much better place to be. Can' t say how much I appreciate this. Do you have any videos on the @classmethod?

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

      Thank you for the positive comments glad the videos are helping. I have not yet covered @classmethods but they are in the pipeline.
      Best wishes
      Phil

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

    I finally understood it!! These animations and drawings really really helped

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

    most clear and in-depth explanation, thank you very much!!!

  • @CarsonNiepper7118
    @CarsonNiepper7118 Před 5 lety +1

    İnanılmaz dərəcədə bəsit, başadüşülür şəkildə izah edir! Thank you very much John, the best explanation of self parameter so far. Especially using id function was very helpful to comprehend the topic. Best regards from Azerbaijan.

  • @andrewpalmer1576
    @andrewpalmer1576 Před 3 lety +2

    Thank you for your great videos, Sir. Very helpful in both content and format.

    • @johnphilipjones
      @johnphilipjones  Před 3 lety

      Glad the videos are helping and thank you for the positive feedback it is appreciated.
      Best wishes
      Phil

  • @irakliycholokashvili3254
    @irakliycholokashvili3254 Před 3 lety +2

    Thank you

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

    Simply amazing! Thank you very much for all those classes! Keep going please! :D

  • @uttarunbhattacharyay8871
    @uttarunbhattacharyay8871 Před 4 lety +2

    thats an amazing explanation ♥️♥️♥️♥️

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

    mate u from the north of england, your video is mint

    • @johnphilipjones
      @johnphilipjones  Před 3 lety

      Yes from the north but now living in Kent. Glad you like the videos. Best wishes
      Phil

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

    So if objects get their own copy of methods, and the methods are called using dot notation after the object name, then what's the point to pass the object as a parameter as the object already have it's copy of method and the object itself knows which object it is?

    • @johnphilipjones
      @johnphilipjones  Před 7 lety

      Every object is identified by its id number and this is passed to self. A programmer can identify the object by the name but the language needs the id number.
      Regards
      Phil

  • @wendyslittleprogram3984

    Thanks for the clearest (and scouse-tinged) explanation I have found so far. So just to check - the 'self' parameter tells that method "please execute the code on the object you're invoked on". Is that not already implied by definition? What would happen if 'self' was not included?

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

      Thank you for the positive feedback it is appreciated. If self was not used then the method would belong to the class. This would mean that each instance would not have their own 'copy' of the method. It is best to regard the class as the template from which each instance (i.e. object is created) for each of these instances to have their own method and data attributes then self has to be used.
      My accent is a mixture of scouse, North Wales, Lancashire and Kent. So it was a good spot.
      Best wishes Phil

  • @elliulla
    @elliulla Před 3 lety

    Thanks for all the great job sir! I hope you are ok. So, every method inside a class that I as a programmer will define and intend to "call" from an instance must have "self" as parameter. Am I right? If so, which type of methods don't need "self" as parameter in its definition?

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

      You can design your class so that it (the class) has methods. This means you do not need to create an instance in order to use such methods. I never design my classes in such a way. I create classes that require the creation of an instance in order to use the instance methods. I regard the class as a factory for creating instances.
      Best wishes
      Phil

  • @yongcuimi4696
    @yongcuimi4696 Před 8 lety

    What's meaning of 'ID' here, is it a python internal numeric identifier assigned to the object or is it a memory pointer/address? If the object were too big, would the memory accommodate it with just a single address?

    • @johnphilipjones
      @johnphilipjones  Před 8 lety

      +Yongcui Mi Every variable is an object and every object has a unique identifier. In CPython the id is the address of the location of the object. Of course the object will take up many locations so the id will be the address of the first location of the object. Consider the following videos:
      czcams.com/video/gxTXgsDsifQ/video.html
      czcams.com/video/EnLAEE1C7X4/video.html
      Regards
      Phil

  • @ElBarosan
    @ElBarosan Před 8 lety

    I think that I got it. User defined classes behave in a different manner than built in classes.
    Even though I store same attribute in two instance of a user defined class there will be created two objects. In a built in class if I store same attribute for two instances there will be created only one object.
    For user defined classes "self" keeps track of objects.
    I'm wrong?

    • @johnphilipjones
      @johnphilipjones  Před 8 lety

      +Tom Sawyer Hello Tom,
      From the perspective of the models I am using if you create n integers with different values, then you have n instances of the integer class (i.e. n objects) each of these objects will have the same methods but each object will have its ‘own copy’ of each method - remember everything in Python is an object.
      Let’s say you create your own class with a number of methods and one attribute (i.e. storage of data). Then you can create n instances of this class (i.e. n objects) and each of these objects exist in their own right and these also have their ‘own copy’ of each of the methods.
      This will become clearer when I have finished the series as I will be introducing the concepts across the series. I am developing the series to more closely match the UML artefacts that are used to design object oriented programs. How Python actually implements its internals does not (in my view) help with this. However, so long as you apply a strict approach to developing your code you can match Python exactly to the UML artefacts.
      Regards
      Phil

  • @mohammedhusain6446
    @mohammedhusain6446 Před 3 lety

    I have one question sir, if self is just pass for instance of class than why its written before every variable that we create in method? Plz tell

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

      As a programmer you type in the code for the class. Elsewhere in the program instances of this class are created i.e. objects. Every object is independent.
      'self' receives a unique identifier for each object created. In order for a variable to 'know' where they are (i.e. to 'know' which object they belong to) self and the dot are placed in front of the variable name. This way each variable is tied to a specific instance (object) of the class.
      Best wishes Phil

    • @mohammedhusain6446
      @mohammedhusain6446 Před 3 lety

      @@johnphilipjones ok sir thank you,
      I have a program here
      class cls:
      x=10
      def __init__(self)
      self.x="This is String"
      obj=cls()
      print(obj.x)
      can you please explain why x which is in __init__ method is running here while calling x but not the x which has value 10..but 10 is printed when i remove the __init__ method

  • @jurgenh9758
    @jurgenh9758 Před 8 lety

    Self is also implied with empty parameters right?

  • @user-xl8uo9gp9p
    @user-xl8uo9gp9p Před 5 lety

    What's the point writing firstobject.method , secondobject.method
    If ultimately we've to provide the object id by ourselves.

    • @johnphilipjones
      @johnphilipjones  Před 5 lety +1

      The programmer (i.e. us) do not provide the id that is passed to self. This is done automatically by the Python language. So each object created gets its own id which is provided by Python.
      Best wishes Phil

    • @user-xl8uo9gp9p
      @user-xl8uo9gp9p Před 5 lety

      @@johnphilipjones sir ,If "self" gets object id,(okay by itself I'm not going into that)then why are we,as a programmer do object.method
      I mean this is redundant,if "which object's method?" Is decided by id provided to self,then why write object.method

    • @johnphilipjones
      @johnphilipjones  Před 5 lety +1

      'self'' is provided by Python it identifies the instance of the class (i.e. the object). A class is a template (or factory) from which objects are 'created'. So if a class has 4 methods each object of the class has these 4 methods but they have their own copy of each. So 'self.method_name()' will invoke the method_name() of the object identified by 'self'
      Best wishes
      Phil

  • @mrcelada
    @mrcelada Před 3 lety

    Ive already watched more than 10 videos about self, but I dont understand why this variable exist, why is it useful for. For now, for me, It only seems a kind of variable for complication sake only.

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

      Think of it this way. A chef makes some scones and uses a cutter to cut out identical shaped scones. The scones are then cooked and they all look identical. What if every scone was given a different flavour through the addition of a food additive. How would the chef know which scone was which? The answer is the chef would not know by looking at them.
      He could have stamped a code on each scone before cooking and have a different code for each scone to reflect the flavour.
      A class is like the scone cutter, the objects of a class are instances - like the scones are instances of the receipe. So how are each object of the class identified? In code they will all have their own name. For the object to carry out its method it has to look at the definition of the method in the class definition. Now the class needs to know what object it is dealing with so when created every object is given a code. Where is this code stored? The answer is it is stored in self and every object will have its own code stored in a version of the variable self.
      If you did not have self where would you store the code?
      Best wishes
      Phil