python module with module-wise global variable -


i made python file several functions in , use module. let's file called mymod.py. following code in it.

from nltk.stem.porter import porterstemmer                       porter = porterstemmer()    def tokenizer_porter(text):                                                                                         return [porter.stem(word) word in text.split()]   

then tried import in ipython , use tokenizer_porter:

from mymod import *  tokenizer_porter('this test') 

the following error generated

typeerror: unbound method stem() must called porterstemmer instance first argument (got str instance instead) 

i don't want put porter inside tokenizer_porter function since feels redundant. right way this? also, possible avoid

from mymod import *  

in case?

many thanks!

to access global variables in python need specify in fucntion global keyword

def tokenizer_porter(text):          global porter                                                                                return [porter.stem(word) word in text.split()]   

Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -