-2

I have written a python class whose constructor takes two lists as arguments.

class nn:
    def __init__(layer_dimensions=[],activations=[]):

        self.parameters = {} 
        self.cache = []      
        self.activations= []

        initialize_parameters(layer_dimensions)
        initialize_activations(activations)

net = nn(list([2,15,2]),list(['relu','sigmoid']))

On trying to pass two lists as arguments in the constructor I get the following error:

TypeError: __init__() takes from 0 to 2 positional arguments but 3 were given

The error states that 3 arguments have been passed but its quite obvious that I've passed only 2.

Ayush Chaurasia
  • 583
  • 4
  • 18

3 Answers3

1

You missed self as instance from which class method is invoked is automatically passed.

class nn:
    def __init__(self, layer_dimensions=[],activations=[]):

        self.parameters = {} 
        self.cache = []      
        self.activations= []

        initialize_parameters(layer_dimensions)
        initialize_activations(activations)

net = nn(list([2,15,2]),list(['relu','sigmoid']))
ravi
  • 10,994
  • 1
  • 18
  • 36
1

I do believe you are missing a self in your constructor

class nn:
    def __init__(self, layer_dimensions=[],activations=[]):

        self.parameters = {} 
        self.cache = []      
        self.activations= []

        initialize_parameters(layer_dimensions)
        initialize_activations(activations)

net = nn(list([2,15,2]),list(['relu','sigmoid']))
user59271
  • 380
  • 2
  • 14
1

Also, it's not a good practice that default arguments to be mutable. You can read more about it here.

Tudor Plugaru
  • 347
  • 2
  • 10