-
Book Overview & Buying
-
Table Of Contents
Metaprogramming with Python
By :
A class is a collection of common attributes and methods that can be reused by creating instances of the class. By creating a class, we define it once and reuse it multiple times, thus avoiding redundancy.
Let us look at what a class can look like. We can consider the Branch entity of ABC Megamart. A Branch can have an ID and an Address. Address can further be detailed into Street, City, State, and Zip code. If we consider Branch as a class, ID, Street, City, State, and Zip code would become its attributes. All operations that can be performed by a branch will become its methods.
A branch can sell products, maintain invoices, maintain inventory, and so on. The generic format of a class is as follows:
Figure 2.2 – Class
A class can be defined as follows:
class ClassName:
'''attributes...'''
...