Learn Python
-
Generator Expressions
We’ve seen list, set and dictionary comprehensions but we haven’t seen tuple comprehensions. That’s because there aren’t any tuple comprehensions in Python. The syntax that you might think would create one, actually creates an iterator. This is known as a generator expression. This code prints some square numbers.
-
Yield: Creating Generator Functions
An easier way to create an iterator in Python (easier than implementing __next__ and __iter__) is to use the yield keyword to create a generator function. Although yield may at first seem counter-intuitive, it really behaves a lot like the return keyword. The difference is, you can iterate over the yielded values. In this code…
-
Exception Error Messages
You can associate a message with an exception when you raise it. Builtin exceptions often have messages associated with them already. To get the message when you catch the exception, just give it a name with the as keyword.
-
Catching Specific Exceptions
If it’s possible that some code may raise multiple different exceptions, we can catch the different types of exception separately. In this example, f_to_c() raises our custom NoInputError if no input is entered, or ValueError if input is entered but can’t be converted to a number. Output if no input entered: Output if text entered…
-
Catching Exceptions
Python programs consist of functions and methods that call each other. Information about which function calls which is stored in an area of memory that Python allocates known as the call stack. If an exception is raised you can catch the exception anywhere up the call stack. So if a function a() calls b() which…
-
Raising Exceptions
You can raise an exception in your Python code using the raise keyword. There are many predefined exceptions in Python. You can also create your own exceptions, typically by creating a subclass of the builtin Exception class. Here’s a program that calculates an area. If you pass negative values to the calculate_area function, it raises…
-
Exceptions Introduction
There are basically two different types of possible error in Python programs. The first type, and often the most difficult to deal with, are errors in your logic. The program works but it doesn’t do what you expect. Your Python is perfectly legitimate and the computer does exactly what you tell it to do, but…
-
Importing Parts of Packages
It’s easy to import only parts of a package in Python. You can import a particular module (file) from a package, or particular classes, functions or constants (variables) from a module. The syntax looks the same or similar in all of these cases. Consider this module: mystuff/mymodule.py If we want to import only the greet…
-
Creating an Iterator
Iterators in Python are objects that you can retrieve other objects from, one by one. For example, you can loop over them using for … in. To turn a class into an iterator, you have to implement the __iter__ and __next__ dunder methods. To stop iteration you raise StopIteration. Iterator Example
-
Package Initialisation
A package in Python is just a directory, which can contain modules (Python files with a .py extension). If you place a file called __init__.py in the directory, it will run when you import the package. mystuff/__init__.py myprog.py output: Importing Packages Directly Instead of importing particular module files from packages, if you place an __init__.py…