提示信息

Python __dir__()

沐年 已关注

__dir__() is a built-in function in Python that returns a list of names in the current local scope or an object's attributes, depending on the context in which it is called.

When __dir__() is called without any arguments, it returns a list of names of the attributes and methods in the current local scope. For example:

  1. print(__dir__())

This will print a list of names in the current local scope, including built-in names, global names, and names defined in the current module.

When __dir__() is called on an object, it returns a list of valid attributes and methods that can be accessed on that object. For example:

  1. my_list = [1, 2, 3]
  2. print(my_list.__dir__())

This will print a list of attributes and methods that can be used on the my_list object, such as append(), count(), and sort().

Note that __dir__() is typically called implicitly when using the dir() function. For example:

  1. print(dir(my_list))

This will have the same effect as calling my_list.__dir__().

    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
沐年 关注 已关注

最近一次登录:2023-10-09 16:05:13   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图