0
Your First Class1 / 4

CRASH COURSE

Your First Class

A class defines how objects are created and how they behave. Each instance can hold its own state.

  • __init__ initializes the instance. self is the instance passed to methods; attributes such as self.name belong to that object.
  • Call a method with object.method(). Python passes the instance automatically, so do not supply self again.
class Counter:
    def __init__(self):
        self.value = 0
    def increment(self):
        self.value += 1
c = Counter()
c.increment()
print(c.value)