MNedelko MNedelko - 8 months ago 23
Python Question

When would I pass in an instance of a class to a method as self?

I am getting started with Python on PyCharm and NLTK and ran into the following question. I have the following code. Text source us a POS-tagged corpus. 'JJ' stands for ADJECTIVE:

Main Script:

for each_element in textSource[indexCounter:]:
indexCounter += 1

if "JJ" in each_element[1]: #this is where we check whether the word's POS-tag is JJ
print("The word we start with: " + str(each_element[0]))
adj_ant_obj_pair = adjectiveobject.Adjective(each_element[0]) #Adj object contains two variables. One is iterationSet (synset)
if adj_ant_obj_pair.iterationSet: #Iteration set is the synset of words we will iterate through
for each_iternationSet in adj_ant_obj_pair.iterationSet:
print("The corresponding synset in Wordnet :" + str(each_iternationSet))
for j in each_iternationSet.lemmas(): #Isolating each synset and find lemma
lemmatized_adjective =
print("Circling through lematized words inside that Synset " + lemmatized_adjective) #Parse the adjective word
if j.antonyms():
for each_antonym in j.antonyms(): #For each antonym (there may be many) we generate a lemmaobject
print("Finding the Antonum of that word :" + str(each_antonym))
#placeHolderList[] =
p = adjectiveobject.Adjective
thewordinQuestion = p.getAdjAntPairs(p, lemmatized_adjective, each_antonym)
#Here is where we should pass on the lemmaobject to the next stage of the processing process
placeHolderList[thewordinQuestion[0]] = thewordinQuestion[1]
else: print('NO ANTONYM')
else: pass

Class adjectiveobject.Adjective:

class Adjective(object):

def __init__(self, taggedInput):
wordnet_lemmatizer = WordNetLemmatizer()
self.taggedWord = wordnet_lemmatizer.lemmatize(taggedInput,'a') #Ensures that the word in question is an adjective
self.iterationSet = wn.synsets(self.taggedWord, 'a') #This is the Synset we need to iteratie through

def getAdjAntPairs(self, servedname, servedantonym):
self.lemmaName = servedname
self.lemmaAntonym =
return(self.lemmaName, self.lemmaAntonym)

So here is my question:

The following line of code requires me to pass on the variable containing the class instance into the class.method call like so:

thewordinQuestion = p.getAdjAntPairs(p, lemmatized_adjective, each_antonym)

If I don't do it [i.e call p.getAdjAntPairs(lemmatized_adjective, each_antonym)] I get the following error:

TypeError: getAdjAntPairs() missing 1 required positional argument: 'servedantonym'

Why is that? Shouldn't the call know which instance I would want to pass in by reading the p.getAdhAntPairs command?

Any advice would be highly appreciated.

cxw cxw

This issue appears to be in your main script:

p = adjectiveobject.Adjective

should be

p = adjectiveobject.Adjective(<whatever tagged input should be>)

(note the trailing parentheses). The line you have saves a reference to the class itself, whereas what I think you want is an instance of the class.