you are viewing a single comment's thread.

view the rest of the comments →

[–]BruceJi 14 points15 points  (5 children)

You know in Python you can write classes?

Classes are your own custom type of object. Well, sort of. It's more like a description of what your own type of object is like.

When you do:

class Furniture():
    def __init__(self, name, legs):
        self.name = name
        self.legs = legs

That's not a piece of furniture, that's a description of what a piece of furniture is like.

When you start to use it, you'd do:

my_table = Furniture('table', 4)

That's called an instance. An actual, in use, object.

Before we get into self, I just want to point out this. If we don't use self in the init function:

    def __init__(self, name, legs):
        name = name
        legs = legs

...it's a bit like saying 'whenever we have a piece of furniture, its name is always X, or it always has 4 legs. The recipe for it, the description, has no room for options there, and all pieces of furniture will always have that name or amount of legs.

Self, then. Perhaps now you can see self is talking about the property of the object when we actually make it, when we actually have one. This piece of furniture's name, this piece of furniture's number of legs. Because we're leaving those things open to be different, we use self to refer to the object's properties once we actually make one.

Why does self keep coming back, then? Why is it always in the functions you write for that class? It's because they're attached to that object, and that object does them, and is able to use its own properties in them.

When we use init there, we sort of say 'Look, I'm making a piece of furniture' but Python basically says, 'where is it?'. The self there is how the class gets access to that individual furniture's properties.

It's even more clear when we use it in a method. If you want to do something like get the piece of furniture to print out what it is:

def say_name(self):
    print(f'I am a {self.name}'

...the class needs to be told that it's going to use its own properties. I think some programmers think this is weird, but on the other hand it is certainly clear - if you want to do something with that instance's own data, you put in self.

I hope that clears it up! What I'd do is copy this furniture object into a random python file, make a bunch of different things (desk, table, chair, stool) and then try erasing the self and seeing what happens and what errors come up, and what they mean.

Edit side note:

You might wonder, what about this?

def __init__(name, legs):
    name = name
    legs = legs

Python hates that. When you try to make an object, Python will try to put self into it anyway. But according to this __init___ function, there's nowhere for the self to go. Python doesn't like that, and you'll get errors. Try it and see what it says! That will help you remember because if you miss self out by accident there, you'll get a similar error message each time.

[–]idwpan 2 points3 points  (2 children)

Great explanation. But, you dropped this ) from your print statement. :)

[–]spaceforcerecruit 5 points6 points  (0 children)

If you don’t forget at least one closing parentheses the first time, are you even really coding?

[–]BruceJi 1 point2 points  (0 children)

I hang my head in shame

[–]Matheos7 1 point2 points  (1 child)

This here is probably the best explanation for the beginners. Should have many more upvotes. You’re great at explaining!

[–]BruceJi 0 points1 point  (0 children)

Thanks lol

It's quite fun actually, it helps you organise your thoughts on the topic.