In Python, 如何重写模块的 str?

huangapple go评论61阅读模式
英文:

in Python, how to overwrite the str for a module?

问题

当用户输入模块名称并按回车键时,会得到如下字符串:

import pandas as pd
pd

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

如何覆盖这个字符串以适用于我的模块?
(请注意,我指的是一个模块,而不是一个类)
例如,在下面添加一段文本:

<module 'mymodule' from '....'>
my additional custom text

我尝试将以下内容添加到我的模块中:

def __str__():
   print('my additional custom text')

以及

def __str__():
   return 'my additional custom text'

def __repr__():
    print("my additional custom text")
    return "my additional custom text"

但什么都没有发生

当然,如果我输入

mymodule.__str__()

我会得到 'my additional custom text'。

英文:

When one types the name of a module and then hits return, one gets a string
like below

import pandas as pd
pd

&lt;module &#39;pandas&#39; from &#39;....&#39;&gt;

How can I overwrite this string for my own module ?
(Please note that I am referring to a module, not to a class)
For example adding a text below like

&lt;module &#39;mymodule&#39; from &#39;....&#39;&gt;
my additional custom text

I tried to add this to mymodule:

def __str__():
   print(&#39;my additional custom text&#39;)

and also

def __str__():
   return &#39;my additional custom text&#39;

def __repr__():
    print(&quot;my additional custom text&quot;)
    return &quot;my additional custom text&quot;

but nothing happens

Of course, if I type

mymodule.__str__()

I get 'my additional custom text'

答案1

得分: 1

@JonSG 正确,它从 __spec__ 中读取 - 我发现所有的逻辑都在 importlib._bootstrap 模块中。

这是如何修改模块名称和路径的方法:

import sys

if __spec__ is not None:
    file_path = 'C:/other_file'
    name = 'new_name'

    __spec__.origin = file_path

    if name != __spec__.name:
        sys.modules[name] = sys.modules[__spec__.name]
        __spec__.name = name

当我导入该文件时:

<module 'new_name' from 'C:/other_file'>

实际上,更改名称需要编辑 sys.modules,所以请小心。但更改路径看起来完全没问题。

英文:

@JonSG was correct, it reads it from __spec__ - I found all the logic for it is in the importlib._bootstrap module.

This is how you'd modify the module name and path:

import sys

if __spec__ is not None:
    file_path = &#39;C:/other_file&#39;
    name = &#39;new_name&#39;

    __spec__.origin = file_path

    if name != __spec__.name:
        sys.modules[name] = sys.modules[__spec__.name]
        __spec__.name = name

When I import the file:

&lt;module &#39;new_name&#39; from &#39;C:/other_file&#39;&gt;

Changing the name actually requires editing sys.modules so be careful. Changing the path however looks completely fine.

答案2

得分: 1

A short answer is: cannot do. Importlib puts the module's repr together with its name and origin properties. Check out the _module_repr_from_spec function implementation.

我相信子类化 ModuleSpec 并覆盖其属性也不是可行的选择,除非您决定不使用 Importlib 并以某种方式自行加载模块。

英文:

A short answer is: cannot do. Importlib puts the module's repr together with its name and origin properties. Check out the _module_repr_from_spec function implementation.

I believe subclassing ModuleSpec and overriding its attributes is also not a viable option, unless you decide to do without Importlib and somehow load modules by yourself.

huangapple
  • 本文由 发表于 2023年4月13日 21:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定