当前位置: 首页 > news >正文

家纺营销型网站网站建设服务费怎么记账

家纺营销型网站,网站建设服务费怎么记账,一款非常不错的seo网站优化公司源码,网站开发心得体会目录 一、功能二、语法和示例 一、功能 dir() 函数获取当前本地作用域中的名称列表或对象的有效属性列表。 二、语法和示例 dir() 函数有两种形式,如果没有实参,则返回当前本地作用域中的名称列表。如果有实参,它会尝试返回该对象的有效属…

目录

  • 一、功能
  • 二、语法和示例

一、功能

dir() 函数获取当前本地作用域中的名称列表或对象的有效属性列表。

二、语法和示例

dir() 函数有两种形式,如果没有实参,则返回当前本地作用域中的名称列表。如果有实参,它会尝试返回该对象的有效属性列表。如果对象有一个名为 __dir__() 的方法,那么该方法将被调用,并且必须返回一个属性列表。dir()函数的语法格式如下:

C:\Users\amoxiang>ipython
Python 3.12.8 (tags/v3.12.8:2dc476b, Dec  3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.32.0 -- An enhanced Interactive Python. Type '?' for help.In [1]: dir?
Docstring:
Show attributes of an object.If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:for a module object: the module's attributes.for a class object:  its attributes, and recursively the attributesof its bases.for any other object: its attributes, its class's attributes, andrecursively the attributes of its class's base classes.
Type:      builtin_function_or_methoddir([object])
参数说明: 
1.object: 对象。该参数为可选参数
2.返回值: 如果没有实参,则返回当前本地作用域中的名称列表。如果有实参,则会返回所有属性和方法,甚至是所有对象默认的内置属性

【示例1】对比导入模块前后的dir()函数返回列表。使用dir()函数输出当前本地作用域中的名称列表,然后导入random和math模块,最后使用dir()函数输出名称列表。代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2025-05-01 7:28
# @Author  : AmoXiang
# @File    : dir_demo.py
# @Software: PyCharm
# @Blog: https://blog.csdn.net/xw1680# ['__annotations__', '__builtins__', '__cached__', '__doc__',
# '__file__', '__loader__', '__name__', '__package__', '__spec__']
print(dir())    # 输出当前本地作用域中的名称列表import random   # 导入random模块
import math     # 导入math 模块
# ['__annotations__', '__builtins__', '__cached__', '__doc__', 
# '__file__', '__loader__', '__name__', '__package__', '__spec__', 'math', 'random']
# 对比输出结果发现,导入random和math 模块后,dir()函数返回结果列表中多了"math"和"random"2个元素。
print(dir())    # 输出当前本地作用域中的名称列表

【示例2】将模块名作为参数传递给dir()函数,输出该模块的属性列表。代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2025-05-01 7:28
# @Author  : AmoXiang
# @File    : dir_demo.py
# @Software: PyCharm
# @Blog: https://blog.csdn.net/xw1680import math
import timeprint(dir(math))  # 输出math模块属性名称列表
print(dir(time))  # 输出time模块属性名称列表# 使用dir(list)会返回列表的所有属性
print([a for a in dir(list) if re.match(r'__\w{,2}__',a)]) # 使用正则表达式,匹配所有包含__**__形式的属性
print([a for a in dir(list) if 're' in a]) # 使用in关键字过滤所有包含"re"字符的属性

【示例3】将不同类型的对象名作为参数传递给dir()函数,输出该模块的属性列表。代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2025-05-01 7:28
# @Author  : AmoXiang
# @File    : dir_demo.py
# @Software: PyCharm
# @Blog: https://blog.csdn.net/xw1680names = ['张三','李四','王五']  # 定义一个列表
info = {'name':'张三','age':18} # 定义一个字典
print(dir(names)) # 输出列表的属性
print(dir(info))  # 输出字典的属性

【示例4】将自定义类对象作为参数传递给dir()函数。代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2025-05-01 7:28
# @Author  : AmoXiang
# @File    : dir_demo.py
# @Software: PyCharm
# @Blog: https://blog.csdn.net/xw1680class Student(object):def __init__(self, name, age):self.name = nameself.age = agedef __dir__(self):'''定义__dir__函数,返回列表'''print('__dir__~~~')# return None TypeError: 'NoneType' object is not iterable# return ['amo', 'jerry', 'paul']return {1}s1 = Student('amo', 18)  # 实例化Student类
print(dir(s1))  # 将实例名称作为参数,传递给调用dir()函数 ==> [1]
print(type(dir(s1)))  # <class 'list'>
print(dir(Student))

【示例5】执行模块中指定的函数。创建一个module_a.py文件,代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2025-05-01 7:57
# @Author  : AmoXiang
# @File    : module_a.py
# @Software: PyCharm
# @Blog: https://blog.csdn.net/xw1680def get_crawl_name():passdef get_crawl_time():passdef crawl_sina():'''爬取新浪'''print('正在爬取新浪数据')def crawl_wechat():'''爬取微信'''print('正在爬取微信数据')def crawl_weibo():'''爬取微博'''print('正在爬取微博数据')def crawl_toutiao():'''爬取头条'''print('正在爬取头条数据')

创建 run.py 文件,使用 import 语句导入 module_a 模块,然后使用 dir() 函数获取模块中的所有属性,如果该属性是以 crawl_ 开头,则调用该函数。代码如下:

# -*- coding: utf-8 -*-
# @Time    : 2025-05-01 7:58
# @Author  : AmoXiang
# @File    : run.py
# @Software: PyCharm
# @Blog: https://blog.csdn.net/xw1680import module_a  # 导入模块list_from_a = dir(module_a)  # 获取模块中所有属性
print(list_from_a)for mod in list_from_a:  # 遍历所有属性if mod.startswith('crawl_'):  # 判断属性名是否以"crawl_"开头f = getattr(module_a, mod)  # 获取对象中以"crawl_"开头的属性值f()  # 调用以"crawl_"开头的方法
http://www.dtcms.com/a/442156.html

相关文章:

  • css其他选择器(精细修饰)
  • 一般设计网站页面用什么软件做引擎网站
  • 生成式 AI 重构内容创作:从辅助工具到智能工厂
  • 华为S5720配置telnet远程
  • 面试复盘:哔哩哔哩、蔚来、字节跳动、小红书面试与总结
  • Your ViT is Secretly an Image Segmentation Model
  • 海口网站建设运营网站开发公司选择
  • 日语学习-日语知识点小记-进阶-JLPT-N1阶段应用练习(4):语法 +考え方17+2022年7月N1
  • RAG:解锁大语言模型新能力的关键钥匙
  • 广州网站建设海珠信科网站建设推广方法
  • Oracle Linux 7.8 静默安装 Oracle 11g R2 单机 ASM 详细教程
  • 旅游公司网站建设方案网站的布局结构三种
  • Django ORM 无法通过 `ForeignKey` 自动关联,而是需要 **根据父模型中的某个字段(比如 ID)去查询子模型**。
  • 吉林省建设厅信息网站网站建设的评价
  • 分布式专题——26.5 一台新机器进行Web页面请求的历程
  • 怎么让别人看到自己做的网站万维网网站301重定向怎么做
  • css样式学习记录
  • 网站服务器关闭网站数据库地址是什么
  • 每日一个C语言知识:C程序结构
  • Amazon RDS:云端数据库管理的革新之路
  • wordpress登录可见站内seo是什么意思
  • STM32简单的串口Bootloader入门
  • 360网站怎么做2核4g做网站
  • 从 “手工作坊” 到 “智能工厂”:2025 年 AI 原生应用重构内容创作产业
  • 做网站平台难在哪里网页翻译不见了
  • Flutter技术栈深度解析:从架构设计到性能优化
  • 学做湘菜的视频网站中国建设企业银行登录网站
  • 【Python进阶】网络爬虫核心技能-第三方IP服务
  • CAS密钥管理系统在汽车行业的核心密钥管理实践——构建智能网联汽车的可信安全底座
  • 宝塔面板登录地址和账密都忘了怎么解决