What is self in Python?
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
Add "Python ASMR" to the video title
Wow! And I was about to comment about how this guy's voice has a perfect tone for ASMR, lol!
the logo "GIRLS" in front of the Tshirt
Yes
Thank you. Well explained. Subbed.
Very interesting and helpful explanation. You are an excellent teacher. Good work !
Thank you! 😊
Can you lower the audio? Its very loud.
I like your calm voice. Thanks 🙏
Thanks, u da man!
People here complaining about you being too silent, bug frankly it like it, its nice and soothing ✌🏻
I thought there was a problem with my hearing aid 😂
No!! He is extremely loud. My eardrums are exploding. 📢🔊🔊
I like the short content. I also like “this” better because I am already used to that from C++ 😂
too silent, you can make the video a bit louder
I think sometime soft soothing voice can be more effective at honing the lesson better than being loud and rambunctious :)
@@ichoudhury007it’s good but at times it’s sleepy 😊
cool stuff, but why are u whispering ?
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
This is why I prefer ruby. Self is the instance, no need to pass it around.
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!
"pyladies", "django girls"
Still, it makes the syntax look gross..... self everwyere as a hidden 1st variable to function...
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?
Absolutely! 👍
You CAN though of course you shoudn't. Here's an example: pym.dev/p/2cn9c/
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.
@@considerallthat3310 nah but I am studying computer engineering abroad and studying C really cleared up a lot of stuff
@@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?
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
@@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
kindly use good mic, your voice is too low
Don't encourage him! His current mic set up gives his voice a perfect ASMR quality. This video helps treat insomnia, lol!👍
Are hungry?
thanks for the guide but bro are you good? You look like you haven't slept in days
I think bro might just be faded
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!
Why are you whispering.
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."
@penguinwizard I disagree with you.
The guy is in fact, whispering. Why should we call it otherwise?
@@penguinwizard why tf is he whispering?
Who cares? It's better than being loud as hell for no reason like a lot of content creators are nowadays
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.
Self refers to the objects that are created within the class. Self==class object.
Yup! That's a good way to explain it. 👍