Today were going to talk about the most exciting topic in Python programming loading stuff into memory. Yep, you heard that right. This is where all the magic happens.
So, let’s say you have a file called `my_module.py` with some code inside it:
# This script defines a function called "my_function" that prints "Hello, world!" when called.
def my_function(): # Defines a function called "my_function"
print("Hello, world!") # Prints "Hello, world!" when the function is called.
Now, imagine that you want to use this function in another Python script. You could copy and paste the code into your new file, but who has time for that? That’s where loading comes in.
First, let’s create a new file called `my_script.py`. This is where we’ll load our module:
# Import the module we want to use
import my_module
# Call the function from our loaded module
my_module.my_function() # Calls the my_function() function from the my_module module
# Note: In order to use a function from a module, we need to import the module first.
# The "import" keyword allows us to access the functions and variables defined in the module.
# We use the dot notation to access the function from the module, in this case "my_module.my_function()".
That’s it! You just loaded your module into memory and called a function from it. It’s like magic, but without all the glitter and sparkles.
Did you know that loading modules can also be done using relative paths? That’s right, No need to specify absolute file paths or worry about where your files are located on disk. Just load them up like this:
# Import the "my_module" module using a relative path
from . import my_module
# Call the "my_function" function from the "my_module" module
my_module.my_function()
# The "." in the import statement indicates that the module is located in the same directory as the current script
# This allows us to avoid specifying an absolute file path and easily load the module
# The "my_function" function is then called from the loaded module, allowing us to use its functionality in our script.
This is especially useful when working with multiple modules in a project, as it allows you to keep your imports organized and tidy.
Now that we’ve covered loading basics, some of the more advanced techniques. For example, did you know that you can load entire packages using the `import` statement? That’s right, No need to import individual modules from a package one by one. Just do this:
# Import the numpy package and assign it an alias "np"
import numpy as np
# Use the random module from the numpy package to generate random numbers
# Set a seed to ensure reproducibility
np.random.seed(123)
# Use the arange function from the numpy package to create an array of numbers from 0 to 4
x = np.arange(5)
# Print the array x
print(x)
This is especially useful when working with large packages like NumPy or Pandas, which have many modules that you might want to use in your code.
Did you know that loading can also be done using the `from … import …` syntax? That’s right, This allows you to selectively load specific functions and classes from a module or package:
# Import specific functions and constants from the math module
from math import sin, cos, pi
# Define a variable x and assign it the value of 3.14
x = 3.14
# Use the sin function from the math module to calculate the sine of x
sin_x = sin(x)
# Use the cos function from the math module to calculate the cosine of pi/2
cos_pi = cos(pi / 2)
# Multiply the sine and cosine values to get the final result
y = sin_x * cos_pi
# Print the result
print(y)
# The purpose of this script is to demonstrate how to selectively import specific functions and constants from a module using the "from ... import ..." syntax. This allows for a more efficient use of memory and avoids loading unnecessary modules.
This is especially useful when working with large modules or packages that have many functions and classes that you might not need in your code.