Reputation: 477
I have the following parent class with kwargs init:
class A(object):
""" Parent class """
def __init__(self, **kwargs):
# connect parameters
self.host = kwargs.get('host', 'localhost')
self.user = kwargs.get('user', None)
self.password = kwargs.get('password', None)
def connect(self):
try:
self.ConnectWithCred( self.host,
self.port,
self.user,
self.password)
except pythoncom.com_error as error:
e = format_com_message("Failed to connect")
raise Error(e)
I want to create an object of 'class A' and call the 'connect' method. How do I go about? I tried the following and it wouldn't run (fyi - I'm a Python newbie):
sub_B = A(self.host = 'example.com', self.port = 22, self.user = 'root',
self.password = 'testing')
sub_B.connect()
Upvotes: 7
Views: 3217
Reputation: 28232
You're creating an instance of A
, not a subclass. Your problem is that your instantiation is a little incorrect, although close.
Remove self
from the keyword arguments:
sub_B = A(host = 'example.com', port = 22, user = 'root', password = 'testing')
This should work fine.
Upvotes: 4