Using an Alias
Often, the name of what you’d like to import can be lengthy. Rather than having to write out an entire name each time you’d like to use it, you can give an “alias” or nickname when importing:
# using the 'as' keyword to create an alias for imports from math import floor as f print( f(2.5) )
Go ahead and run the cell. We’ll get the same output as we do in the previous two cells, except this time we were able to reference the floor function as just the letter “f “. This is because of how we wrote our import statement using the “as” keyword. You can rename anything that is imported, although it’s generally best to only do so on larger names.
Creating Our Own Module
Now that we know how to import and call a module, let’s create our own. Go ahead and open any text editor you have on your computer like Notepad or TextEdit. Write the following code in the file, and save it within the same folder that your “Week_09” file is located, with the name “test.py”. If the two files aren’t in the same directory, it produces an error:
# creating our own module in a text editor
# variables to import later length = 5 width = 10 # functions to import later def printInfo(name, age): print( "{ } is { } years old.".format(name, age) )
See Figure 9-1 for an example of what the code will look like within a text editor.
Figure 9-1. test.py module with code in text editor (notepad++)
You’ve just written your first module! Remember that modules are nothing more than code written in other files that we can import in any of our projects. Now let’s see how to use them.
Using Our Module in Jupyter Notebook
In any other circumstance, you’d import the variables and function we wrote in test.py with the import and from keywords. Jupyter Notebook, however, works a little differently when using modules that you’ve created. We’ll use the “run” command in order to load in the entire module that we’ve created. After we run the file, we can use the variables and functions that we wrote within the module. Let’s check out how to do so:
# using the run command with Jupyter Notebook to access our own modules
%run test.py print(length, width)
printInfo("John Smith", 37) # able to call from the module because we ran the file in Jupyter above
Go ahead and run the cell. You’ll notice that we’re able to output the variables and function print statement that we declared within our test.py module. Keep in mind that the run command runs the file as if it were a single cell. Any function calls or print statements within our module would run immediately. To test this out, try putting a print statement at the bottom of the module. When you work in a development environment (IDE), you’ll write the import as you would normally, like the following:
>>> from test import length, width, printInfo
ChAptEr 9 AdvAnCEd topiCs ii: ComplExity
This is just how Jupyter Notebook works with files that we create.
Note you can place any modules you create within the python folder on your hard drive. once the files are there, they can be accessed normally rather than using the run command.
Do'stlaringiz bilan baham: |