惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Register - Security
The Register - Security
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
量子位
C
Check Point Blog
博客园 - 【当耐特】
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
博客园 - Franky
N
Netflix TechBlog - Medium
Webroot Blog
Webroot Blog
A
About on SuperTechFans
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
D
Docker
S
Security Affairs
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
V2EX - 技术
V2EX - 技术
Jina AI
Jina AI
Help Net Security
Help Net Security
L
LangChain Blog
P
Proofpoint News Feed
The Cloudflare Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Schneier on Security
Schneier on Security
Recent Announcements
Recent Announcements
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
Microsoft Security Blog
Microsoft Security Blog
H
Heimdal Security Blog
P
Proofpoint News Feed
O
OpenAI News
H
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
爱范儿
爱范儿
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - tsembrace

Pandas数据分析教程,由浅入深讲解(五)Pandas IO操作 Pandas数据分析教程,由浅入深讲解(四) Pandas数据分析教程,由浅入深讲解(三) Pandas数据分析教程,由浅入深讲解(二) Pandas数据分析教程,由浅入深讲解(一) B站黑马Python+AI零基础入门(二)AI应用-实战 B站黑马Python+AI零基础入门(一)AI应用-基础 B站清华128小时Python高级教程一(C3深入类和对象) B站清华128小时Python高级教程一(C1、C2) B站python入门学习---第二阶段第二章数据库、SQL和MySQL B站python入门学习---第二阶段第一章类和对象 B站python入门学习---第一阶段第十章 数据可视化 B站python入门学习---第一阶段第八~九章 文件操作、异常、模块与包 B站python入门学习---第一阶段前七章回顾复习(二) B站python入门学习---第一阶段前七章回顾复习(一) Python入门学习(十)第十四章其他 Python入门学习(九)Python的高级语法与用法(四)装饰器 Python入门学习(九)Python的高级语法与用法(三)函数式编程 Python入门学习(九)Python的高级语法与用法(二)闭包 Python入门学习(九)Python的高级语法与用法(一)枚举 Python入门学习(八)JSON、反序列化、序列化 Python入门学习(七)高级部分:正则表达式(二) Python入门学习(七)高级部分:正则表达式 Python入门学习(六)高级部分:面向对象 Python入门学习(五)函数
Python入门学习(四)流程控制 包与模块
tsembrace · 2025-05-06 · via 博客园 - tsembrace

#流程控制

account = "qiyue"
password = "123456"

user_account = input("please input your account:")

user_password = input("please input your password:")

if user_account == account  and user_password ==password:
    print("Success!")
else:
    print("Wrong!")

 #包与模块

1、Python项目的组织结构:包——>模块——>类——>(函数、变量)
2、Python包和普通文件夹的区别在于存在一个__init__.py文件
3、__init__文件一般用于外部文件导入其所在包时自动运行的,__init__文件中可以通过设置__all__设定可以被导入的模块
4、也可以使用__init__文件统一集中导入多个模块的处理
5、import导入的本质就是执行被导入的模块
"""
       This is t4.file!!!
"""
#模块的内置变量
#dir函数用来反馈模块内的变量
infos = dir()
print(infos)
print(__name__)   #描述文件名
print(str(__package__))    #包名
print(__doc__)     #注释
print(__file__)   #文件的路径

需要注意的是,当文件直接作为入口程序时,__doc__打印的则是“__main__”,如果是被导入的模块文件,则打印文件名。

#__name__的典型应用


"""
    Make a script both importable and executable
    让一个脚本文件即可以作为一个模块也可以自己作为一个可执行程序文件
"""
if __name__ == "__main__":
    print("This is app.")

print("This is a module.")

#关于__package__

一般入口文件(即当前执行的文件)本身是没有包的概念的(不归属于哪个包),产生包的概念是入口文件中导入了其他模块文件,此时这些模块文件就有了包的概念。

所以入口文件中打印__package__会显示“None”,而其所导入的文件如打印则会显示包名。

#相对导入和绝对导入

#以下为通过绝对路径导入的方式
#必须从“顶级包”(即与入口程序平级的包)索引开始
#入口程序中不能使用相对路径导入模块:原因如下
#1、入口程序的__name__会被强制修改为"__main__"
#2、相对路径的本质就是通过当前文件的__name__去寻径的
#综上1、2,故无法找到__main__路径
#import不能使用相对路径,from  import可以使用相对路径
import package2.package4.m2
print(__name__)
#from  import可以使用相对路径
#.表示同级    ..表示上一级   ...表示上上一级
from ..m4 import m
m = 2
print(__package__)
print(m)