Lecture

How to Declare a Class in Python

In Python, a class is declared using the class keyword.

Class names are typically written in Pascal Case, where the name starts with a capital letter.

Pascal Case involves merging multiple words into one, capitalizing the first letter of each word.

For example, total amount becomes TotalAmount, with spaces removed and the first letter of each word capitalized.


Basic Structure of a Class Declaration

The basic structure for declaring a class is as follows.

Basic Structure of Class Declaration
class ClassName: # Definition of attributes and methods ...

After the class keyword, write the class name followed by a colon (:) to define the attributes and methods of the class.

A class created this way can be used to instantiate an object by placing parentheses (()) after the class name.

Creating a Class Object
test = ClassName()

The above code demonstrates how to create an object named test using the ClassName class.

If the class has a constructor method (__init__) that accepts parameters, you need to pass arguments when creating an object.

Creating a Class Object with a Constructor Method
class ClassName: def __init__(self, arg1, arg2): # Contents of the constructor method ... test = ClassName(arg1, arg2)

As shown above, you must pass arg1, arg2 that match the type and number of arguments defined in the constructor method __init__.

Mission
0 / 1

What is the most appropriate word to fill in the blank?

Generally, classes in Python are defined in format.
Kebab case
Snake case
Camel case
Pascal case

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result