- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
python 模块可以有 __repr__ 吗?这个想法是做类似的事情:
import mymodule
print mymodule
编辑:精度:我的意思是用户定义 repr!
最佳答案
简答:基本上答案是否定的。
但是您不能使用文档字符串找到您正在寻找的功能吗?
testmodule.py
""" my module test does x and y
"""
class myclass(object):
...
test.py
import testmodule
print testmodule.__doc__
长答案:
您可以在模块级别定义自己的 __repr__
(只需提供 def __repr__(...
),但您必须这样做:
import mymodule
print mymodule.__repr__()
获得您想要的功能。
看看下面的 python shell session :
>>> import sys # we import the module
>>> sys.__repr__() # works as usual
"<module 'sys' (built-in)>"
>>> sys.__dict__['__repr__'] # but it's not in the modules __dict__ ?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: '__repr__'
>>> sys.__class__.__dict__['__repr__'] # __repr__ is provided on the module type as a slot wrapper
<slot wrapper '__repr__' of 'module' objects>
>>> sys.__class__.__dict__['__repr__'](sys) # which we should feed an instance of the module type
"<module 'sys' (built-in)>"
所以我认为问题在于这些 slot wrapper objects这(从链接中可以读取的内容)的结果是绕过了通常的“python”查找项目属性的方式。
对于这些类方法,CPython 返回指向这些对象上相应方法的 C 指针(然后将其包装在插槽包装器对象中,以便从 python 端调用)。
关于python - python 模块可以有 __repr__ 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1725515/
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
如何创建特殊方法 __repr__我可以打印的地方,例如 '6 of spades'或 'Q of diamonds' ? 我如何从 namedtuple 访问数据,请记住我有一个 list的 nam
是否可以使下面代码中的__repr__返回数据帧? 下面的代码将抛出 TypeError: __str__ returned non-string (type DataFrame) 因为 - 我猜 -
我正在研究 Python 元编程。 class FormMetaClass(type): def __new__(cls, clsname, bases, methods):
背景 我编写了一个修饰函数来修改给定类的 __repr__,这样当调用一个类实例时,它的所有属性都会打印给用户。当在下面示例中的 Container 类中使用时,装饰器 __repr__dec 的行为
我想自己写__repr__对于我定义的某些类。我希望它类似于默认的 ,除了那里有一些其他细节。我如何重现 东西? 最佳答案 参见 http://docs.python.org/reference/d
所以我有一个类方法,我想用它来绘制字典及其值: def __repr__ (self): for row in zip(*([ky]+map(str,val) for ky,val
处理持久化对象的 __repr__() 函数的最佳方法是什么?例如,表示数据库中的一行(关系或对象)。 根据 Python 文档,__repr__() 应该返回一个字符串,该字符串将使用 eval()
我正在开发一个主要返回闭包而不是传统的基于类的方法的 Python 项目。例如: def term(token): def fn(text): return (...)
这两种方法有区别吗? 例如, from datetime import date today = date(2012, 10, 13) repr(today) 'datetime.date(2012,
用成员变量x和y为类Foo实现__repr__,有没有办法自动填充字符串?无效的示例: class Foo(object): def __init__(self, x, y):
我有一个表示接口(interface)的抽象基类。此类的子类将此类的其他子类存储为属性。 例如: class AbstractBase(object): pass class Child(Ab
这个问题在这里已经有了答案: Purpose of __repr__ method? (6 个答案) 关闭 4 年前。 我正在尝试自己学习 python,但我坚持使用 __repr__ 函数。尽管我
我有以下(示例)代码: class _1DCoord(): def __init__(self, i): self.i = i def pixels(self):
print OBJECT 调用OBJECT.__str__(),那么什么时候调用OBJECT.__repr__()?我看到当 OBJECT.__str__() 不存在时 print OBJECT 调用
最近,__repr__() 遇到了很多麻烦。 , format() , 和编码。 应该输出__repr__()是编码还是 unicode 字符串? __repr__() 的结果是否存在最佳编码?在 P
def __repr__(self): return '' % ( self.__class__.__name__, self.urlconf_name, self.app_name,
( python 3.5.2) 我已经为我的一个类定义了 __repr__ 如下: class d(): def __init__(self): self._values =
我已经创建了我的 Node 和 Stack 类,但我不知道如何在 Stack 类中显示 repr 以便能够打印堆栈中当前的所有项目?我一直在尝试连接节点,但我不确定如何连接,因为 Stack() 不允
我通常写一个__repr__作为展示如何首先重新创建实例的一种方式。例如: class Component: def __init__(self, start, end): s
我是一名优秀的程序员,十分优秀!