Yik Yak icon
Join communities on Yik Yak Download
can someone please explain how self works in python with classes and objects because i genuinely have been googling for hours and watching lectures and i still. cannot. grasp why this is efficient
upvote 35 downvote

default user profile icon
Anonymous 1w

self refers to the specific instance of an object. If a class is Human, then self.name means each object created from that class has its own name. For example, human1.name = "John" and human2.name = "Matt" store different values. self can be named anything, but it’s the standard convention. A class is the blueprint, and an object is an instance created from it that holds its own data.

upvote 34 downvote
default user profile icon
Anonymous 1w

Idk but i found that chatgpt can sometimes give an explanation i understand better so maybe check there

upvote 22 downvote
default user profile icon
Anonymous 1w

The idea behind using classes with `self` and methods instead of just having a bunch of freestanding functions is that it helps organize your code better Typically when you have some data about something, you have a lot of functions that you want to use to do stuff with it Classes serve to logically group that data and its related functions into a single entity. This also means you don't have to pass all that data as arguments to the function, it's implicitly accessible via self

upvote 3 downvote
default user profile icon
Anonymous replying to -> #4 1w

I would write out some examples for you but they don't fit

upvote 2 downvote
default user profile icon
Anonymous replying to -> #3 1w

thank u!!

upvote 5 downvote
default user profile icon
Anonymous replying to -> #4 1w

thank u sm!!

upvote 1 downvote
default user profile icon
Anonymous replying to -> #1 1w

If i really dont get something then i ask chat to explain it like im five

upvote 5 downvote
default user profile icon
Anonymous replying to -> #5 1w

ive seen this method but it never really clicked for me i dont really need it dumbed down i usually just need it worded differently

upvote 1 downvote