What is self in Python?

Sdílet
Vložit
  • čas přidán 4. 06. 2024
  • Python's "self" is really just a variable that points to the current instance of our class. Every method you define must accept "self" as its first argument.
    Read an article version of this video at pym.dev/what-is-self/
    Find more Python screencasts at pym.dev/screencasts/
    00:00 A Python class with self in each method
    00:28 Do we need that self argument?
    01:22 What is self?
    02:26 But what does self mean? Could we rename it?
    03:10 Recap: self is the first method of every Python class
    #python #oop
  • Věda a technologie

Komentáře • 45

  • @saluberis
    @saluberis Před 3 měsíci +41

    Add "Python ASMR" to the video title

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

      Wow! And I was about to comment about how this guy's voice has a perfect tone for ASMR, lol!

    • @daved8698
      @daved8698 Před měsícem

      the logo "GIRLS" in front of the Tshirt

    • @malakhassan9816
      @malakhassan9816 Před měsícem

      Yes

  • @InfoSecGSO
    @InfoSecGSO Před 26 dny

    Thank you. Well explained. Subbed.

  • @SoniaChavez-je7hq
    @SoniaChavez-je7hq Před 3 měsíci +5

    Very interesting and helpful explanation. You are an excellent teacher. Good work !

  • @techrazor3280
    @techrazor3280 Před 19 dny

    Can you lower the audio? Its very loud.

  • @abuali5513
    @abuali5513 Před 3 měsíci

    I like your calm voice. Thanks 🙏

  • @mathewmcfool
    @mathewmcfool Před 3 měsíci +1

    Thanks, u da man!

  • @ACCESSZERO1
    @ACCESSZERO1 Před 4 měsíci +7

    People here complaining about you being too silent, bug frankly it like it, its nice and soothing ✌🏻

    • @Code4Life400
      @Code4Life400 Před 3 měsíci

      I thought there was a problem with my hearing aid 😂

    • @techrazor3280
      @techrazor3280 Před 19 dny

      No!! He is extremely loud. My eardrums are exploding. 📢🔊🔊

  • @hudatolah
    @hudatolah Před 2 měsíci +1

    I like the short content. I also like “this” better because I am already used to that from C++ 😂

  • @vorfreu
    @vorfreu Před 6 měsíci +11

    too silent, you can make the video a bit louder

    • @ichoudhury007
      @ichoudhury007 Před 5 měsíci +6

      I think sometime soft soothing voice can be more effective at honing the lesson better than being loud and rambunctious :)

    • @Thekingslayer-ig5se
      @Thekingslayer-ig5se Před 25 dny

      @@ichoudhury007it’s good but at times it’s sleepy 😊

  • @itsomarka
    @itsomarka Před 3 měsíci +1

    cool stuff, but why are u whispering ?

  • @PythonMorsels
    @PythonMorsels  Před 3 měsíci

    For more on Python's classes, see this playlist: czcams.com/video/wIq3yYBEZWQ/video.html
    There's also a text-based version of each of those videos at www.pythonmorsels.com/articles/class/
    And for practice making your own class, try out this Python Morsels exercise: www.pythonmorsels.com/exercises/7b02e2aae0634dc4a6f8cec15d1e1a8a/?level=beginner

  • @chrishabgood8900
    @chrishabgood8900 Před 2 měsíci

    This is why I prefer ruby. Self is the instance, no need to pass it around.

    • @PythonMorsels
      @PythonMorsels  Před 2 měsíci +2

      As a method caller, we don't actually need to pass it around because Python does the "passing" part for us. But as a method definer, we do need to accept it as an argument which will be passed in.
      That's not to say Python's style is better though...it definitely takes some adjusting to get used to that passed-in self!

  • @shanmugaraj4416
    @shanmugaraj4416 Před 24 dny

    "pyladies", "django girls"

  • @KW-jj9uy
    @KW-jj9uy Před 4 měsíci +3

    Still, it makes the syntax look gross..... self everwyere as a hidden 1st variable to function...

  • @akashbharadwaj1
    @akashbharadwaj1 Před 3 měsíci

    awesome explanation man... really great..
    so can we have different words for each instance? like can we have self in one method..... this in other method... or xyz in another method..... can we have different ones in each?
    i am not asking about best coding pracitces.. i am asking CAN we do it? can it be done?

    • @PythonMorsels
      @PythonMorsels  Před 3 měsíci

      Absolutely! 👍
      You CAN though of course you shoudn't. Here's an example: pym.dev/p/2cn9c/

  • @yoavmor9002
    @yoavmor9002 Před 5 měsíci +7

    Yet another Python quirk that becomes IMMEDIATELY simpler when you write a bit of C.
    There is no such thing as a function that can magically access some local variable. For the function to have access to the data of the object, the object must be passed into the function. That is self.

    • @yoavmor9002
      @yoavmor9002 Před 3 měsíci

      @@considerallthat3310 nah but I am studying computer engineering abroad and studying C really cleared up a lot of stuff

    • @akashbharadwaj1
      @akashbharadwaj1 Před 3 měsíci

      @@yoavmor9002 awesome explanation man... really great..
      so can we have different words for each instance? like can we have self in one method..... this in other method... or xyz in another method..... can we have different ones in each?
      i am not asking about best coding pracitces.. i am asking CAN we do it? can it be done?

    • @akashbharadwaj1
      @akashbharadwaj1 Před 3 měsíci +1

      i want to add that your comment made the video meaningful to be very honest. i read your comment and watched again.. and everything was so clear .. thank you sooo sooo much. really. god bless you thanks

    • @yoavmor9002
      @yoavmor9002 Před 3 měsíci

      @@akashbharadwaj1 Yeah it's possible, all it is is a name for a parameter. You can call it self in the __init__ function, call it stringable in the __repr__ function and call it this_object in a func function

  • @AkshaySharma-vm9sp
    @AkshaySharma-vm9sp Před 3 měsíci

    kindly use good mic, your voice is too low

    • @wisdomlounge4452
      @wisdomlounge4452 Před měsícem

      Don't encourage him! His current mic set up gives his voice a perfect ASMR quality. This video helps treat insomnia, lol!👍

  • @derciferreira2523
    @derciferreira2523 Před 3 měsíci

    Are hungry?

  • @hw_was_taken423
    @hw_was_taken423 Před 5 měsíci +2

    thanks for the guide but bro are you good? You look like you haven't slept in days

    • @thomasstien2134
      @thomasstien2134 Před 4 měsíci

      I think bro might just be faded

    • @wisdomlounge4452
      @wisdomlounge4452 Před měsícem

      Ironically, his voice has that perfect ASMR pitch that, while apparently not getting enough nightly rest, he can easily talk others into getting the kind of profoundly deep sleep that he himself needs!

  • @daddyddesign
    @daddyddesign Před 6 měsíci +5

    Why are you whispering.

    • @penguinwizard
      @penguinwizard Před 6 měsíci +11

      Don't attack people personally, you could have rephrased that comment as "The sound is very low, and I am having issues hearing. Please fix."

    • @renatooliveira0266
      @renatooliveira0266 Před 5 měsíci +7

      ​@penguinwizard I disagree with you.
      The guy is in fact, whispering. Why should we call it otherwise?

    • @rubyciide5542
      @rubyciide5542 Před 5 měsíci +2

      @@penguinwizard why tf is he whispering?

    • @IronShredder
      @IronShredder Před 4 měsíci +7

      Who cares? It's better than being loud as hell for no reason like a lot of content creators are nowadays

    • @megsman4749
      @megsman4749 Před 4 měsíci +1

      There probably should have been a disclaimer. Like ‘I live in a studio apartment with room mates who are sleeping.’ Or something. But that’s just me.

  • @AlexandreFerreira-jb2jl
    @AlexandreFerreira-jb2jl Před 2 měsíci

    Self refers to the objects that are created within the class. Self==class object.

    • @PythonMorsels
      @PythonMorsels  Před 2 měsíci

      Yup! That's a good way to explain it. 👍