1

I have looked at What is a clean pythonic way to have multiple constructors in python, but I need further assistance since I'm still a newbie. The example there is when you have just one parameter, and I have four.

Let's say that I have a class:

class Word:

    def __init__(self, wordorphrase, explanation, translation, example):
        self.wordorphrase = wordorphrase
        self.explanation = explanation
        self.example = example
        self.translation = translation

Now I can create Word objects only by passing four parameters when creating an object, for instance:

w = Word(self.get_word(), self.get_explanation(), self.get_translation(), self.get_example())

How should I modify my __init__ method so that I can create objects by:

w = Word()
Community
  • 1
  • 1

1 Answers1

0

One way that you could accomplish this is by specifying default arguments for your four variables that are not self. I tested the following code and was able to create instances of the word class without passing any arguments.

class Word:

    def __init__(self, wordorphrase=None, explanation=None, translation=None, example=None):
        self.wordorphrase = wordorphrase
        self.explanation = explanation
        self.example = example
        self.translation = translation