鱼C论坛

 找回密码
 立即注册
查看: 2122|回复: 7

[已解决]关于dir()函数输出的问题

[复制链接]
发表于 2017-11-18 11:25:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我导入了scipy模块,想看看里面有什么属性和方法,然后在shell中输入dir(scipy)得到一个列表,然后肉眼观察是否有misc方法,发现找不到,但应该是存在的才对。
然后我逐个打印输出这个列表的元素才发现是有的,然后再在ipython中试了试,dir(scipy),发现得到的结果是没有显示misc方法。。
这是什么bug吗?

反正import scipy.misc都可以的,但是却不能用dir方法直接看到这个方法,我就担心以后看其它模块所包含的方法也可能会显示不全。
最佳答案
2017-11-18 11:42:23
misc应该是一个包。在scipy目录里一个名为misc的文件夹,里面有一个__init__.py文件。
  1. import scipy
复制代码

导入的是scipy文件夹里的__init__.py里的东西。
截图01.png
截图00.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-11-18 11:42:23 | 显示全部楼层    本楼为最佳答案   
misc应该是一个包。在scipy目录里一个名为misc的文件夹,里面有一个__init__.py文件。
  1. import scipy
复制代码

导入的是scipy文件夹里的__init__.py里的东西。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-11-18 11:44:48 | 显示全部楼层
Python3\Lib\site-packages\scipy\misc


misc是scipy的子包,并不是属性
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-11-18 12:03:23 | 显示全部楼层
SixPy 发表于 2017-11-18 11:44
misc是scipy的子包,并不是属性

哦哦,这样呀。我看到了那个文件夹。可是为啥我用print函数输出dir(scipy)的每一项,却能看到misc?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-18 12:25:26 | 显示全部楼层
sunny霉神 发表于 2017-11-18 12:03
哦哦,这样呀。我看到了那个文件夹。可是为啥我用print函数输出dir(scipy)的每一项,却能看到misc?

  1. 'misc' in dir(scipy)
复制代码


值是 False 啊,说明 dir(scipy) 里没有 'misc' 啊。

具体您“用print函数输出dir(scipy)的每一项”的代码是怎么样的?

我用:
  1. for item in dir(scipy):
  2.     print(item)
复制代码


并没有看到 'misc' 。

https://docs.python.org/3.6/library/functions.html#dir

dir([object])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

If the object has a method named __dir__(), this method will be called and must return the list of attributes. This allows objects that implement a custom __getattr__() or __getattribute__() function to customize the way dir() reports their attributes.

If the object does not provide __dir__(), the function tries its best to gather information from the object’s __dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattr__().

The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:

If the object is a module object, the list contains the names of the module’s attributes.
If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-18 13:51:11 | 显示全部楼层
Amazon 发表于 2017-11-18 12:25
值是 False 啊,说明 dir(scipy) 里没有 'misc' 啊。

具体您“用print函数输出dir(scipy)的每一 ...

不好意思哈。我那时候先
  1. from scipy import misc
  2. for i in dir(scipy):
  3.         print(i)
复制代码

所以才能看到misc
没有那句from import就不能在dir(scipy)的结果里看到misc了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-18 14:21:27 | 显示全部楼层
sunny霉神 发表于 2017-11-18 13:51
不好意思哈。我那时候先

所以才能看到misc


这样子啊,明白了。谢谢。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-11-19 10:58:49 | 显示全部楼层
Amazon 发表于 2017-11-18 14:21
这样子啊,明白了。谢谢。

我该谢谢你才对,嘿嘿。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-18 08:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表