python float 类不会出现在类层次结构中,除非“prodded”
如果我使用此函数打印出 python 类层次结构,由于某种原因,类型“float”不会出现在输出中。
def printHier(cls, indent = 0, tab = " "):
print "%s%s" % (tab*indent, cls.__name__)
try:
subclasses = cls.__subclasses__()
except TypeError:
subclasses = cls.__subclasses__(cls)
subclasses.sort(key = lambda v: v.__name__)
for subcls in subclasses:
printHier(subcls, indent = indent + 1)
printHier(object)
如果我定义这个附加函数(如下)并在调用第一个函数之前调用它,那么 float 就会出现。谁能解释这种奇怪的行为?一些Python类有什么偷懒的地方吗?我想知道是否还缺少其他一些课程。
def tweak(cls):
"""
for some reason "float" doesn't show up in hierarchy unless
we "prod" it...
"""
superclasses = cls.__mro__
tweak(float)
If I use this function to print out a python class hierarchy for some reason the type "float" doesn't show up in the output.
def printHier(cls, indent = 0, tab = " "):
print "%s%s" % (tab*indent, cls.__name__)
try:
subclasses = cls.__subclasses__()
except TypeError:
subclasses = cls.__subclasses__(cls)
subclasses.sort(key = lambda v: v.__name__)
for subcls in subclasses:
printHier(subcls, indent = indent + 1)
printHier(object)
If I define this additional function (below) and call it before calling the first, then float shows up. Can anyone explain this odd behavior? Is there something lazy about some python classes? I'm wondering if it might be missing some other classes too.
def tweak(cls):
"""
for some reason "float" doesn't show up in hierarchy unless
we "prod" it...
"""
superclasses = cls.__mro__
tweak(float)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎是Python 2.6.1的问题。
float
在我的安装(2.6.4 和 2.7,均在 Windows 上)以及 Jay 和 Samplebias 测试的其他较新版本上显示。我在 CPython 变更日志中查找了相关内容,但找不到任何似乎相关的内容。
It seems to be a problem with Python 2.6.1.
float
shows on my installations (2.6.4 and 2.7, both on Windows) and on other newer versions tested by Jay and samplebias.I looked for something relevant in the CPython changelog, but I couldn't find anything that seems to be related.