OneBite.Dev - Coding blog in a bite size

declare a class in python

Code snippet on how to declare a class in python

class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'

This code creates a Python class called ‘MyClass’. The class starts with the class keyword, followed by the class name and a colon. The class contains one attribute (variable) called i, which is assigned the integer value of 12345. There is also a function inside the class called f, which returns the string "hello world" whenever it’s called. The self keyword is used to refer to the object itself in the class definition. This is required for all functions in a class, and all functions in a class need at least one argument. Finally, the docstring for the class is written after triple quotes, which is an optional but recommended practice.

python