Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
learn-python/src/modules/test_modules.py at master · trekhleb/learn-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
trekhleb
learn-python
Repository navigation
Code
Issues
17
(17)
Pull requests
26
(26)
Actions
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
learn-python
/
src
/
modules
/
test_modules.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
97 lines (75 loc) · 4.33 KB
master
Breadcrumbs
learn-python
/
src
/
modules
/
test_modules.py
Copy path
Top
File metadata and controls
Code
Blame
97 lines (75 loc) · 4.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Modules.
@see: https://docs.python.org/3/tutorial/modules.html
As your program gets longer, you may want to split it into several files for easier maintenance.
You may also want to use a handy function that you’ve written in several programs without copying
its definition into each program.
To support this, Python has a way to put definitions in a file and use them in a script or in an
interactive instance of the interpreter. Such a file is called a module; definitions from a module
can be imported into other modules or into the main module (the collection of variables that you
have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name
with the suffix .py appended. Within a module, the module’s name (as a string) is available as the
value of the global variable __name__.
When the interpreter executes the import statement, it searches for module in a list of
directories assembled from the following sources:
- The directory from which the input script was run or the current directory if the interpreter is
being run interactively
- The list of directories contained in the PYTHONPATH environment variable, if it is set. (The
format for PYTHONPATH is OS-dependent but should mimic the PATH environment variable.)
- An installation-dependent list of directories configured at the time Python is installed
The resulting search path is accessible in the Python variable sys.path, which is obtained from a
module named sys:
>>> import sys
>>> sys.path
@see: https://realpython.com/python-modules-packages/
"""
# This does not enter the names of the functions defined in fibonacci_module directly in the
# current symbol table; it only enters the module name fibonacci_module there.
import
fibonacci_module
# There is a variant of the import statement that imports names from a module directly into the
# importing module’s symbol table. For example:
# pylint: disable=reimported
from
fibonacci_module
import
fibonacci_at_position
,
fibonacci_smaller_than
# There is even a variant to import all names that a module defines. This imports all names except
# those beginning with an underscore (_). In most cases Python programmers do not use this facility
# since it introduces an unknown set of names into the interpreter, possibly hiding some things you
# have already defined.
# >>> from fibonacci_module import *
# If the module name is followed by as, then the name following as is bound directly to the
# imported module:
import
fibonacci_module
as
fibonacci_module_renamed
# It can also be used when utilising from with similar effects:
from
fibonacci_module
import
fibonacci_at_position
as
fibonacci_at_position_renamed
# When a module named spam is imported, the interpreter first searches for a built-in module with
# that name. If not found, it then searches for a file named spam.py in a list of directories
# given by the variable sys.path. sys.path is initialized from these locations:
#
# - The directory containing the input script (or the current directory when no file is specified).
# - PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
# - The installation-dependent default.
def
test_modules
():
"""Modules"""
assert
fibonacci_module
.
fibonacci_at_position
(
7
)
==
13
assert
fibonacci_at_position
(
7
)
==
13
assert
fibonacci_module_renamed
.
fibonacci_at_position
(
7
)
==
13
assert
fibonacci_at_position_renamed
(
7
)
==
13
assert
fibonacci_module
.
fibonacci_smaller_than
(
100
)
==
[
0
,
1
,
1
,
2
,
3
,
5
,
8
,
13
,
21
,
34
,
55
,
89
]
assert
fibonacci_smaller_than
(
100
)
==
[
0
,
1
,
1
,
2
,
3
,
5
,
8
,
13
,
21
,
34
,
55
,
89
]
assert
fibonacci_module_renamed
.
fibonacci_smaller_than
(
10
)
==
[
0
,
1
,
1
,
2
,
3
,
5
,
8
]
# If you intend to use a function often you can assign it to a local name.
fibonacci
=
fibonacci_module
.
fibonacci_smaller_than
assert
fibonacci
(
100
)
==
[
0
,
1
,
1
,
2
,
3
,
5
,
8
,
13
,
21
,
34
,
55
,
89
]
# The built-in function dir() is used to find out which names a module defines. It returns a
# sorted list of strings.
assert
dir
(
fibonacci_module
)
==
[
'__builtins__'
,
'__cached__'
,
'__doc__'
,
'__file__'
,
'__loader__'
,
'__name__'
,
'__package__'
,
'__spec__'
,
'fibonacci_at_position'
,
'fibonacci_smaller_than'
,
]
You can’t perform that action at this time.