1

I just started learning Python, and I'm starting to get into Object-Oriented Programming. I wrote the following code which simply draws shapes using the turtle module, but I still don't quite understand the use of the "self" keyword:

import turtle

class Shape:
    def __init__(self, sides, angle, color="white", thickness=1):
        self.sides = sides
        self.angle = angle
        self.color = color
        self.thickness = thickness

    def draw(self):
        for x in range(self.sides):
            turtle.color(self.color)
            turtle.pensize(self.thickness)
            turtle.forward(100)
            turtle.left(90)
            if self.angle > 90:
                turtle.right(self.angle - 90)
            elif self.angle < 90:
                turtle.left(90 - self.angle)

hexagon = Shape(6,120,"Red", 5)
hexagon.draw()
  • 1
    What about it are you confused about? You use it several times here. Also note, it's not a keyword. `self` could be replaced by any name; `self` is just a strong convention. It's the first parameter position that's important. – Carcigenicate Dec 07 '20 at 15:33

1 Answers1

1

self refers to the instance of the class from inside the class. https://www.programiz.com/article/python-self-why

Here is the content of the link:

If you have been programming in Python (object-oriented programming) for some time, then you have definitely come across methods that have self as their first parameter.

Let us first try to understand what this recurring self parameter is. What is self in Python?

In object-oriented programming, whenever we define methods for a class, we use self as the first parameter in each case. Let's look at the definition of a class called Cat.

class Cat:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def info(self):
        print(f"I am a cat. My name is {self.name}. I am {self.age} years old.")

    def make_sound(self):
        print("Meow")

In this case all the methods, including init, have the first parameter as self.

We know that class is a blueprint for the objects. This blueprint can be used to create multiple numbers of objects. Let's create two different objects from the above class.

cat1 = Cat('Andy', 2)
cat2 = Cat('Phoebe', 3)

The self keyword is used to represent an instance (object) of the given class. In this case, the two Cat objects cat1 and cat2 have their own name and age attributes. If there was no self argument, the same class couldn't hold the information for both these objects.

However, since the class is just a blueprint, self allows access to the attributes and methods of each object in python. This allows each object to have its own attributes and methods. Thus, even long before creating these objects, we reference the objects as self while defining the class.

Kroustou
  • 659
  • 5
  • 14
  • Assuming you call the method normally. `self` can actually be anything if you call the method as though it were static. – Carcigenicate Dec 07 '20 at 15:37