educative.io

Stacks concept class variable doubt?

class Stack():

def __init__(self):

    self.items = []

here ‘items’ is called a class variable but is it not an instance variable. Please correct me if I am wrong but anything under a method is an instance variable.


Type your question above this line.

Course: https://www.educative.io/collection/10370001/5474278013140992
Lesson: https://www.educative.io/collection/page/10370001/5474278013140992/5212270311243776

Hi @Khyati_Tuwani

Instance Variable: An instance of a class owns instance variables. The value of an instance variable can be different depending on the instance with which the variable is associated.

For class No, ‘items’ is an instance variable because it can have different values across multiple instances of a class.

In the above code, ‘items’ is associated with the self, an instance variable. If the ‘items’ is not associated with self, it will be a local variable.

I hope this may help you. :blush:

1 Like