Python 模块属性:名称、文档、文件、字典

Python 模块有描述它的属性。属性执行一些任务或包含一些关于模块的信息。一些重要属性解释如下:

_ 名称 _ 属性

__name__属性返回模块的名称。默认情况下,文件的名称(不包括扩展名。py)是 name 属性的值。

Example: name Attribute

>>> import math
>>> math.__name__
'math' 

同样,它给出了自定义模块的名称。

Example: name Attribute

>>> hello.__name__
'hello'

但是,这可以通过为该属性分配不同的字符串来修改。如下图改变hello.py

Example: Set name

def SayHello(name):
    print ("Hi {}! How are you?".format(name))
__name__="SayHello" 

现在检查__name__属性。

>>> import hello
>>> hello.__name__
'SayHello'

__name__属性的值是 Python 交互 Shell上的__main__

>>> __name__
'__main__'

当我们运行任何 Python 脚本(即模块)时,其__name__属性也被设置为__main__。 例如,在 IDLE 中创建以下 welcome.py。

Example: welcome.py

print("__name__ = ", __name__) 

按 F5 在 IDLE 中运行上述 welcome.py。您将看到以下结果。

Output in IDLE:

>>> __name__ =  __main__

但是,当导入该模块时,其__name__被设置为其文件名。 现在,用以下内容导入新文件 test.py 中的欢迎模块。

Example: test.py

import welcome
print("__name__ = ", __name__) 

现在按 F5 在 IDLE 中运行 test.py。__name__属性现在是“欢迎”。

Example: test.py

__name__ =  welcome 

该属性允许 Python 脚本用作可执行文件或模块。

更多信息请访问 Python 中的 main。

_ 文档 _ 属性

doc 属性表示用模块代码编写的文档字符串(docstring)行。

Example:

>>> import math  

>>> math.__doc__ 

'This module is always available.  It provides access to the mathematical functions defined by the C standard.' 

考虑以下脚本保存为test.py模块。

test.py

"""This is docstring of test module"""
def SayHello(name):
    print ("Hi {}! How are you?".format(name))
    return 

__doc__属性将返回一个在模块代码开头定义的字符串。

Example:

>>> import test
>>> test.__doc__
'This is docstring of test module' 

_ 文件 _ 属性

__file__是一个可选属性,保存从中加载它的模块文件的名称和路径。

Example: file Attribute

>>> import io
>>> io.__file__
'C:\\python37\\lib\\io.py' 

_ 字典 _ 属性

__dict__属性将返回模块属性、函数和其他定义及其各自值的字典对象。

Example: dict Attribute

>>> import math
>>> math.__dict__
{'__name__': 'math', '__doc__': 'This module is always available.  It provides a
ccess to the\nmathematical functions defined by the C standard.', '__package__':
 '', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': Modu
leSpec(name='math', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='
built-in'), 'acos': <built-in function acos>, 'acosh': <built-in function acosh>
, 'asin': <built-in function asin>, 'asinh': <built-in function asinh>, 'atan':
<built-in function atan>, 'atan2': <built-in function atan2>, 'atanh': <built-in
 function atanh>, 'ceil': <built-in function ceil>, 'copysign': <built-in functi
on copysign>, 'cos': <built-in function cos>, 'cosh': <built-in function cosh>,
'degrees': <built-in function degrees>, 'erf': <built-in function erf>, 'erfc':
<built-in function erfc>, 'exp': <built-in function exp>, 'expm1': <built-in fun
ction expm1>, 'fabs': <built-in function fabs>, 'factorial': <built-in function
factorial>, 'floor': <built-in function floor>, 'fmod': <built-in function fmod>
, 'frexp': <built-in function frexp>, 'fsum': <built-in function fsum>, 'gamma':
 <built-in function gamma>, 'gcd': <built-in function gcd>, 'hypot': <built-in f
unction hypot>, 'isclose': <built-in function isclose>, 'isfinite': <built-in fu
nction isfinite>, 'isinf': <built-in function isinf>, 'isnan': <built-in functio
n isnan>, 'ldexp': <built-in function ldexp>, 'lgamma': <built-in function lgamm
a>, 'log': <built-in function log>, 'log1p': <built-in function log1p>, 'log10':
 <built-in function log10>, 'log2': <built-in function log2>, 'modf': <built-in
function modf>, 'pow': <built-in function pow>, 'radians': <built-in function ra
dians>, 'remainder': <built-in function remainder>, 'sin': <built-in function si
n>, 'sinh': <built-in function sinh>, 'sqrt': <built-in function sqrt>, 'tan': <
built-in function tan>, 'tanh': <built-in function tanh>, 'trunc': <built-in fun
ction trunc>, 'pi': 3.141592653589793, 'e': 2.718281828459045, 'tau': 6.28318530
7179586, 'inf': inf, 'nan': nan} 

dir() 是一个内置函数,也返回一个模块中所有属性和函数的列表。

Know Module Attributes and Methods

在 Python 文档中了解更多模块属性。***

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/248968.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 13:30
下一篇 2024-12-12 13:30

相关推荐

  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python函数名称相同参数不同:多态

    Python是一门面向对象的编程语言,它强烈支持多态性 一、什么是多态多态是面向对象三大特性中的一种,它指的是:相同的函数名称可以有不同的实现方式。也就是说,不同的对象调用同名方法…

    编程 2025-04-29
  • Python中取出字典中对应键的值

    如何使用Python在字典中获取特定键的值?这是Python编程中必须掌握的技能之一。本文将通过多个方面来详细讲解Python如何取出字典中对应键的值。 一、通过键名获取值 当我们…

    编程 2025-04-29
  • 光模块异常,SFP未认证(entityphysicalindex=6743835)——解决方案和

    如果您遇到类似optical module exception, sfp is not certified. (entityphysicalindex=6743835)的问题,那么…

    编程 2025-04-29
  • Python如何遍历字典中的key和value

    本文将详细讲解Python中如何遍历字典中的key和value,包括多种遍历方式以及在遍历过程中的一些应用场景。 一、遍历字典中的key和value 在Python中,字典是一种无…

    编程 2025-04-29
  • Vant ContactList 增加属性的实现方法

    在使用前端UI框架Vant中的ContactList组件时,我们有时需要为此组件增加一些个性化的属性,来满足我们特定的需求。那么,如何实现ContactList组件的增加属性功能呢…

    编程 2025-04-29
  • 全面解读数据属性r/w

    数据属性r/w是指数据属性的可读/可写性,它在程序设计中扮演着非常重要的角色。下面我们从多个方面对数据属性r/w进行详细的阐述。 一、r/w的概念 数据属性r/w即指数据属性的可读…

    编程 2025-04-29
  • Python模块下载与安装指南

    如果想要扩展Python的功能,可以使用Python模块来实现。但是,在使用之前,需要先下载并安装对应的模块。本文将从以下多个方面对Python模块下载与安装进行详细的阐述,包括使…

    编程 2025-04-29
  • Python编程三剑客——模块、包、库

    本文主要介绍Python编程三剑客:模块、包、库的概念、特点、用法,以及在实际编程中的实际应用,旨在帮助读者更好地理解和应用Python编程。 一、模块 1、概念:Python模块…

    编程 2025-04-29
  • 如何使用pip安装模块

    pip作为Python默认的包管理系统,是安装和管理Python包的一种方式,它可以轻松快捷地安装、卸载和管理Python的扩展库、模块等。下面从几个方面详细介绍pip的使用方法。…

    编程 2025-04-28

发表回复

登录后才能评论