0

Ok this should be trivial, but i am stuck- the code is existing and should be fine.

 class Connect(object):
    def __init__(self, database, user, password, host, port):
        super(Connect, self).__init__()
        self.database = database
        self.user = user
        self.password = password
        self.host = host
        self.port = port

 class Process(Connect):
    def __init__(self, **kwargs):
        super(Process, self).__init__(**kwargs)

I can instantiate Connect easily

local_connection=Connect(database, user, password, host, port)

How do I instantiate Process?

If I do Process(database, user, password, host, port) - Error is - TypeError: init() takes exactly 6 arguments (1 given)

If I do

Process(local_connection) 

Error is - TypeError: init() takes exactly 1 argument (2 given)

If i try

Process()

Errorr is - TypeError: init() takes exactly 6 arguments (1 given)

Illusionist
  • 5,204
  • 11
  • 46
  • 76
  • 2
    I would tend to disagree with your assertion that the existing code is fine. The forwarding from `Process` to `Connect` is IMHO simply broken. – Ulrich Eckhardt Nov 07 '16 at 20:18
  • 2
    Why does `Process` only take `self` and `**kwargs`? Why not give it the same signature as the class it inherits from? Or positional `*args`? – jonrsharpe Nov 07 '16 at 20:18
  • Thanks, I thought it was not correct .. but people have been using it for a while and I incorrectly assumed its probably fine . – Illusionist Nov 07 '16 at 20:32

1 Answers1

1

You can fix this in two ways:

  1. create a Process's object along with argument's name as:

    >>> Process(database='database', user='user', password='password', host='host', port='port')
    <__main__.Process object at 0x7f4510150a10>
    
  2. OR, Use *args along with **kwargs in Process.__init__() as:

    class Process(Connect):
        def __init__(self, *args, **kwargs):
            super(Process, self).__init__(*args, **kwargs)
    

    and just pass param to __init__() without arguments as:

    >>> Process('database', 'user', 'password', 'host', 'port')
    <__main__.Process object at 0x7f4510150950>
    

Please also refer: What does ** (double star) and * (star) do for parameters? for knowing the difference between *args and **kwargs.

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126