Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add more variables to the initialization after I import a module to a class in python?

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 )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading