1
我做了一個python文件,其中有幾個函數,我想用它作爲模塊。假設這個文件叫做mymod.py。下面的代碼在裏面。Python模塊與模塊明智的全局變量
from nltk.stem.porter import PorterStemmer
porter = PorterStemmer()
def tokenizer_porter(text):
return [porter.stem(word) for word in text.split()]
然後我試圖將其導入的IPython和使用tokenizer_porter:
from mymod import *
tokenizer_porter('this is test')
生成以下錯誤
TypeError: unbound method stem() must be called with PorterStemmer instance as first argument (got str instance instead)
我不想把看門的tokenizer_porter函數內因爲它感覺多餘。什麼是正確的方式來做到這一點?此外,是否有可能避免
from mymod import *
在這種情況下?
非常感謝!
它爲我指定它。謝謝。在這種情況下是否可以避免從mymod導入*?我嘗試從mymod導入tokenizer_porter並沒有工作。 – nos
添加全球后你嘗試過嗎?因爲它應該工作 – aershov
你是對的。非常感謝! – nos