Skip to main content

Command Palette

Search for a command to run...

TDI: The Great Divide — 'def' vs 'class

Updated
2 min read
A
5th grade tech explorer. Learning Python and robotics to build a high tech robot. Designing a new language based on Python to push the boundaries of AI.

While writing my lines (codes), I found out 2 similar keywords: def (define) and class (classification).

Many Python explorers like me will be very curious about their differences, so after some research, I found out that they have a lot of differences. Let's look at their roles when building a tool with Python.

( 1 ) def (Create a Function)

Effects: You "tell" the computer to do something.

Where to Use: When you need to calculate a specific equation, for example, like density calculation, 'def' can be the quickest way to calculate.

Disadvantage: It's like a microphone; you give it something, and it sends back the result, but it can't "remember" anything.

( 2 ) class (Classification / Blueprint)

Effects: You are creating a blueprint that has memory and power.

Where to Use: When you need to invent a system, this system needs to control a lot of things, like coordinates, color, or what time you should open this system.

Advantage: What is different from 'def' is that it can remember what's in its "mind", so you don't need to put in a parameter every time you start it.

Comparison Summary:

  • def is Action-oriented (Focusing on the process).

  • class is Entity-oriented (Focusing on the object and its state).

As a Python explorer, I use def to perform specific tasks and class to build the structure of my application.

https://play.juicemind.com/sandbox/eLaGJkwaSz94hXB60X3o