Sobes.tech
Junior — Middle

What keyword in Python is used to declare a variable inside a class constructor that is only used within the __init__ block?

sobes.tech AI

Answer from AI

In Python, the keyword self is used to declare a variable inside a class constructor that belongs to the instance of the object. For example:

class MyClass:
    def __init__(self, value):
        self.variable = value  # instance variable

The variable self.variable is accessible in all class methods through the self object. Variables declared without self inside __init__ are local and only accessible within that method.