I would like in this blog to shed some light over the utility of a class. Though I will be using Python, these concepts apply regardless the programming language used.
In fact, it is most likely that you have already used a class. For example, when we create an int variable, we are actually creating an object (the variable) of data type int which in turn, is defined in the int class.
Conceptually, the class Int could look something like this:
In this section I will create a class called clsPerson which will represent a person (duh!):
- fname – this is a string which holds the person’s first name
- lname – this is a string which holds the person’s last name
- bday – holds the person’s birthdate
- __init__ – this is the constructor
- GetAge() – this function returns the age of the person
The Python code to create this class is the following – refer to the comments inline:
I suspect that after looking at the code, you would still be a bit confused regarding the self keyword … Check the comments below:
The loop generates the following output:
In the future, if we need to extend functionality, we just need to add additional attributes and functions to the class without changing existing code.
- Classes create data types
- A class is a data structure which encapsulates attributes and functions
- Allows us to reference the objects as a logical unit
- Provide scalability
Rafael A. Couto Cabral • LinkedIn Profile
Cisco | F5 | VMware Certified • PRINCE2 Practitioner
Originally posted 2017-11-23 01:51:09.