This is my third blog in “Let’s talk Python”. I am grateful to Shoolini University for providing me with such an ecstatic opportunity to learn something new. This week we learnt about Error and exception handling. Aside from this, we also learnt about generators, decorators, modules, packages, and file handling. It was a great week as I got to know more and more about this intriguing language.
Errors and exceptions are just like our daily life problems. Small problems that we ignore grow up to be big hurdles of our lives. That's why in the Zen of python it is advised to never ignore an error. We can handle these errors sophisticatedly to give users the best experience.
In python, unlike other languages we don’t have a catch block, rather we are provided with an “except” block. The functioning, however, is pretty much the same. We can pass a different combination of handlers to handle our program and also the user can design his/ her exception handlers.
The new thing here is that we are provided with an assertion error that does not require try and except. It will just check the given condition, if the condition is true then code works fine otherwise an error message is displayed.
“Divisor cannot be zero” is the error message that we are going to display if the num2 equals zero. This is just a single line error just like a lambda function().
Then we learnt about generators and decorators.
Generator: A generator is a function that returns an object (iterator) which we can iterate over (one value at a time). A generator function must have one or more yield statements in it.
Decorator: It is what it says, it modifies the values of the original function.
In this function: hi() is a decorator function. It is mandatory for a decorator function to be mentioned as “@decoratorname” before the “to-be-decorated” function.
Besides this, we studied about packages and modules.
A module is just a file with a “.py” extension that means any program we make can behave as a python module. Syntax: import <modulename>
A package is like a record of modules or furthermore packages.
Syntax: from <packagename> import <modulename>
Then we studied various file handling techniques. At the end of the week, we were acquainted with OOPS. The coming week we shall be studying that. I will update you with what I learn in the next part of “let’s talk Python”