在 Python 中,如何覆盖模块的 str?

问题描述 投票:0回答:0

当输入模块名称然后回车时,会得到一个字符串 像下面

import pandas as pd
pd

<module 'pandas' from '....'>

如何为我自己的模块覆盖这个字符串?例如在下面添加一段文字 like

<module 'mymodule' from '....'>
etc, etc, etc...

我试图将其添加到我的模块中:

def __str__():
   print('something')

还有

def __str__():
   return 'something'

但什么也没发生

当然,在这两种情况下,如果我输入

mymodule.__str__()

我得到“东西”

python string overwrite
© www.soinside.com 2019 - 2024. All rights reserved.