I have one small question regarding Python classes and variables.
Why 'self' keyword 'spoils' the work of counter variable in the first example while in 2nd example (which is pretty similar in terms of mechanism to the first one) 'self' keyword does not do anything 'wrong' in terms of the outcome:
# Counter does not work properly (incrementation) if 'self' keyword is provided before the variable named 'counter'.
# Instead of 'self' keyword we shall use class name, to make this work 'ExampleOne.counter += 1'.
class ExampleOne:
counter = 0
def __init__(self, name, surname):
self.name = name
self.surname = surname
self.counter += 1
# It works, meaning list is being updated. Even if 'self' keyword is provided before variable named 'list'.
class ExampleTwo:
list = []
def __init__(self, name, surname):
self.name = name
self.surname = surname
self.list.append([self.name, self.surname])