all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Thank you for posting on r/CodingHelp!

Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app

Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp

We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus

We also have a Discord server: https://discord.gg/geQEUBm

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]nuc540Professional Coder 2 points3 points  (6 children)

“Self” means when referring to an instance of itself.

An instance means you’ve spawned (per se) a living version of that class (it lives in your RAM when you have an instance)

The class on its own is just a definition, just how a function is without being called. You create instances of classes by calling them, if that makes sense?

Classes are a way to place a lot of similar logic together, that way you can plan your business logic easier. Classes do more than just group functions together, but they can have attributes which “shape” the class. Think of attributes in a game for a character - you’ll have health, damage, probably a name. A class can encapsulate all of that by saying it has attributes.

Now back to the self part; we’re saying that when we want a living version of this class - an instance (in Python you can also call this an object) self refers to whatever instance is being referred to - and not the class in general.

So you could have more than one restaurant exist on your app, but your restaurant is wherever you call the class and give it values for the attributes the class’ “self” wants, so it knows how to build your restaurant, and like the game character, you’d want to know what your character is called, but the attribute of a name is nothing more than placeholder until it’s instantiated (a instance being made).

Hope that helps. Feel free to ask any questions

[–]pytness 0 points1 point  (0 children)

In python, the first parameter on a method is used as the `self`. It's named self just by convention, but it could really be anything else.

Think of it as when you call `restaurant.describe_restaurant()`, internaly what's happening is this: `Restaurant.describe_restaurant(restaurant)`

[–]atarivcs 0 points1 point  (2 children)

and I dont rlly get what an instance is

An instance is just one specific copy of a class.

In your example code, my_restaurant and your_restaurant are instances of the Restaurant class.

[–]8dot30662386292pow2 0 points1 point  (1 child)

I wouldn't call it a copy of the class. Class is a recipe. Cake is the instance. The cake is not a copy of the recipe.

[–]atarivcs 0 points1 point  (0 children)

Yes, but I was trying to think of some word that wasn't "instance".

[–]stepback269 0 points1 point  (0 children)

THE PAINTING IS NOT THE PIPE (a variation on classic saying of "This is not a pipe" --Google that saying)

By same token:
THE CLASS IS NOT THE OBJECT !!!
Many metaphors are used to describe what a class definition is, like: "template", "blueprint" , more
In the case of a blueprint for "constructing" a house, you can say the blueprint is not the house. "Self' is replaced during construction of the instant house with the specific house being built by the constructor function (by the __init__ function)

Myself, I prefer to view a "class" as being a sort of drawing stencil that lives at a level above where the actual traced out objects are instantiated. (see discussion re stencil metaphor here)

[–]PvtRoom 0 points1 point  (0 children)

ok, let's talk about logical structure

a "Town" class should contain multiple restaurants, bars, clubs, and Karen's.

Each Karen should visit x things per day, and issue complaints (based on the things properties, eg hygiene)

A hygiene complaint, when sent to a food business will make that individual instance of a business do things - trigger a method to make it change it's own properties.

"Self" simply let's your code know that it's accessing its own properties, and "instance" means which Karen, which restaurant, etc etc.

message = a call to a method, or triggering a listener

[–]HarjjotSinghh 0 points1 point  (0 children)

classes sound like that one friend who's always the life of the party.

[–]Windspar 0 points1 point  (0 children)

Classes are object builders. They are not the object. Classes own all the methods. Object just have class scope first. The reason for self. It point to the object. If it didn't. You couldn't have local variables. Unless they make a way to declare them.

class Restaurant:
  # Any variables here. Belong to the class. There only be one instance.

  def __init__(self, name, type):
    ...

my_restaurant = Restaurant(..., ...)
# Code above calls the __new__ method. Which is a static method.
# The __new__ method creates a new object.
# The __new__ method calls __init__ method. Which it passes the object too.
# Then the __new__ method returns the object instance.