49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
# Modules in Python are just Python files with a .py extension. The name of the module is the same as the file name. A Python module can have a set of functions, classes, or variables defined and implemented. The example above includes two files:
|
|
# mygame/
|
|
# mygame/game.py
|
|
# mygame/draw.py
|
|
# The Python script game.py implements the game. It uses the function draw_game from the file draw.py, or in other words, the draw module that implements the logic for drawing the game on the screen.
|
|
# Modules are imported from other modules using the import command. In this example, the game.py script may look something like this:
|
|
|
|
# game.py
|
|
# import the draw module
|
|
import draw
|
|
|
|
def play_game():
|
|
pass
|
|
|
|
def main():
|
|
result = play_game()
|
|
draw.draw_game(result)
|
|
|
|
# this means that if this script is executed, then
|
|
# main() will be executed
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|
|
# A namespace is a system where every object is named and can be accessed in Python. We import the function draw_game into the main script's namespace by using the from command.
|
|
# game.py
|
|
# import the draw module
|
|
from draw import draw_game #To import all objects instead of "draw_game" write '*'
|
|
|
|
def main():
|
|
result = play_game()
|
|
draw_game(result)
|
|
|
|
|
|
# Exercise
|
|
# In this exercise, print an alphabetically sorted list of all the functions in the re module containing the word find.
|
|
import re
|
|
|
|
# Your code goes here
|
|
import re
|
|
|
|
# Your code goes here
|
|
find_members = []
|
|
for member in dir(re):
|
|
if "find" in member:
|
|
find_members.append(member)
|
|
|
|
print(sorted(find_members))
|