0

I am having a problem calling my class and function to my main code. I created a class and do the method in it and just call it in my main code.

This is my class:

from nltk.corpus import wordnet
class syn:
    def synon():
        f = open('filename', "r")
        first = f.readline()
        print("1. ", first)
        input_ans = input('Answer: ')
        synonyms = []
        for syn in wordnet.synsets(first):
            for l in syn.lemmas():
                synonyms.append(l.name())
        if input_ans == first:
            score += 0
            print("Wrong")
        elif input_ans in set(synonyms):
            print("Correct")
            score += 1
        else:
            score += 0
            print("Wrong")

This is my main code:

import os
from synonyms import syn
from nltk.corpus import wordnet

a = syn()
a.synon()

It does not print anything.

2
  • 2
    Python is not Java. You didn't have to wrap functions in classes. They work happily on their own. Commented Nov 25, 2021 at 6:34
  • you have typo a.syno() -> a.synon() Commented Nov 25, 2021 at 6:48

1 Answer 1

1

Basically, you are missing the self argument. See here or here for more logic behind why it's needed. If you're going to wrap the function in a class, you might find __init__ and other "dunder" methods interesting.

Here is a toy example that will do what you want:

class syn:
    def member_function(self):
        print("hello from syn")
        
a = syn()
a.member_function()
Sign up to request clarification or add additional context in comments.

2 Comments

There are no class attributes in this case. Method should have @staticmethod decorator.
It worked now. I just have a typo. What does it mean when I have no attribute in function? I am having an error from part of score.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.