Python:如何在导入时替换一段代码

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

Python : How to replace a piece of code when importing

问题

我有两个文件,main.py和module.py。Module.py被从另一个程序(Qt Designer)中擦除并重新编写,这意味着我无法自己修改它。

由于Pyside-uic问题,当我导入Module.py时,我需要将所有对一个函数(Qstring())的调用替换为另一个函数(str())。

我该如何做?

英文:

I have two files, main.py and module.py. Module.py is erased and re-written from another program (Qt designer), meaning I can't modify it myself.

Because of a Pyside-uic issue, I need to replace all calls to a function ( Qstring() ) by another one ( str() ) when I import Module.py.

How can I do that ?

答案1

得分: 2

我曾经以这种方式完成过:

在我的主程序内部:

import module-fix.py

module-fix.py

import module.py as module
def fix_func():
  pass
module.func = fix_func

希望能有所帮助。

英文:

I did it once in this way:

Inside my main program:

import module-fix.py

module-fix.py

import module.py as module
def fix_func():
  pass
module.func = fix_func

Hope it will help

huangapple
  • 本文由 发表于 2020年1月3日 19:38:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577965.html
匿名

发表评论

匿名网友

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

确定