🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

class Keyword

AI & DATA SCIENCE // class-keyword

class defines a new class — a blueprint for creating objects that bundle data (attributes) and behavior (methods) together.

Syntax

class ClassName:
    def __init__(self, ...):
        ...
    def method(self, ...):
        ...

Deep Dive Course

class creates a new type object and binds it to a name, the same way def creates a function object. Everything indented under the class line is the class body, typically a set of method definitions, functions that take self as their first parameter, plus optional class-level attributes shared by all instances. Calling the class like a function creates a new instance and automatically invokes __init__() to set up that instance's own data.

1Understanding class Keyword

class creates a new type object and binds it to a name, the same way def creates a function object. Everything indented under the class line is the class body, typically a set of method definitions, functions that take self as their first parameter, plus optional class-level attributes shared by all instances. Calling the class like a function creates a new instance and automatically invokes __init__() to set up that instance's own data.

💡

Class names conventionally use CapWords (PascalCase), while functions and variables use snake_case — following this convention makes it instantly clear from a name alone whether you're looking at a class or a regular function.

editor.html
class Dog:
    def __init__(self, name):
        self.name = name
    def bark(self):
        return f"{self.name} says Woof!"

d = Dog("Rex")
print(d.bark())
localhost:3000

2Practical Example

Here is a real-world application of class Keyword showing how it is used in production Python code.

editor.html
class Counter:
    count = 0
    def __init__(self):
        Counter.count += 1

a, b, c = Counter(), Counter(), Counter()
print(Counter.count)
localhost:3000

3Best Practices

Follow these guidelines when working with class Keyword:

1. Use PascalCase for class names to visually distinguish them from functions and variables

2. Keep a class focused on a single, clear responsibility rather than bundling unrelated behavior together

3. Prefer composition (one class holding an instance of another) over deep inheritance hierarchies when it better models the relationship

⚠️

Tip: Class names conventionally use CapWords (PascalCase), while functions and variables use snake_case — following this convention makes it instantly clear from a name alone whether you're looking at a class or a regular function.

editor.html
class Dog:
    def __init__(self, name):
        self.name = name
    def bark(self):
        return f"{self.name} says Woof!"

d = Dog("Rex")
print(d.bark())
localhost:3000

Examples

Example 01Basic Usage
class Dog:
    def __init__(self, name):
        self.name = name
    def bark(self):
        return f"{self.name} says Woof!"

d = Dog("Rex")
print(d.bark())
Example 02Advanced Example
class Counter:
    count = 0
    def __init__(self):
        Counter.count += 1

a, b, c = Counter(), Counter(), Counter()
print(Counter.count)

Best Practices

  • Use PascalCase for class names to visually distinguish them from functions and variables
  • Keep a class focused on a single, clear responsibility rather than bundling unrelated behavior together
  • Prefer composition (one class holding an instance of another) over deep inheritance hierarchies when it better models the relationship

Interview Question

What's the difference between a class attribute (defined directly in the class body) and an instance attribute (assigned to self inside __init__)?

Hint: Think about who owns the value and how many copies exist.

A class attribute is defined once, directly inside the class body, and is shared by every instance of the class unless a specific instance overrides it with its own attribute of the same name. An instance attribute, typically set inside __init__ by assigning to self, belongs to that one specific object, so each instance can have its own independent value. Mutating a shared mutable class attribute through one instance can therefore unexpectedly affect all other instances that haven't overridden it.

Exercises

MediumPractice using class Keyword in a real scenario.
View Solution
class Dog:
    def __init__(self, name):
        self.name = name
    def bark(self):
        return f"{self.name} says Woof!"

d = Dog("Rex")
print(d.bark())

Frequently Asked Questions

What's the difference between a class attribute (defined directly in the class body) and an instance attribute (assigned to self inside __init__)?

A class attribute is defined once, directly inside the class body, and is shared by every instance of the class unless a specific instance overrides it with its own attribute of the same name. An instance attribute, typically set inside __init__ by assigning to self, belongs to that one specific object, so each instance can have its own independent value. Mutating a shared mutable class attribute through one instance can therefore unexpectedly affect all other instances that haven't overridden it.

Related Functions

initself-parameterinheritance