I am trying to add more variables inside the initialization bracket after importing a module and I get an error,__init__ takes 3 positional arguments but four were given
. How do I fix this?
#Module1
class(first):
def __init__(self, word, count):
self.word = word
self.count = 0
from Module1 import first
class second(first):
def __init__(self, word, count, option):
super().__init__(word, count, option)
def new(word):
types = type(word)
if types == str:
print(word)#if the word is a string print the word
count = 1#increment the value to 1
option1 = "first attempt"
word1 = "coding"
result = second(word1, 0, option1)
res = result.new(word1)
print(res)
I am getting an error(TypeError: __init__() takes 3 positional arguments but 4 were given )
CodePudding user response:
You can add the attribute option to the class second:
class first(object):
def __init__(self, word, count):
self.word = word
self.count = 0
class second(first):
def __init__(self, word, count, option):
super().__init__(word, count)
self.option = option
def new(self, word):
types = type(word)
if types == str:
print(word) # if the word is a string print the word
self.count = 1 # increment the value to 1
option1 = "first attempt"
word1 = "coding"
result = second(word1, 0, option1)
res = result.new(word1)
print(res)
CodePudding user response:
What you are saying is that second
needs an additional member variable. This is one of the patterns available through OOP, called "programming by difference"
. You just need to get second
to do this:
class second(first):
def __init__(self, word, count, option):
super().__init__(word, count)
self.option = option
btw there are other problems with the rest of your code. If you need to, you can ask a separate question about that on stackoverflow.