# Defining a Base class to be shared among many other classes later:
class Base(dict):
"""Base is the base class from which all the class will derrive.
"""
name = 'name'
def __init__( self):
"""Initialise Base Class
"""
dict.__init__(self)
self[Base.name] = ""
# I create an instance of the Base class:
my_base_instance = Base()
# Since a Base class inherited from a build in 'dict' the instance of the class is a dictionary. I can print it out with:
print my_base_instance Results to: {'name': ''}
# Now I am defining a Project class which should inherit from an instance of Base class:
class Project(object):
def __init__(self):
print "OK"
self['id'] = ''
# Trying to create an instance of Project class and getting the error:
project_class = Project(base_class)
TypeError: __init__() takes exactly 1 argument (2 given)
3 Answers
there are two mistakes in your code:
1) Class inheritance
class Project(Base): # you should inherit from Base here...
def __init__(self):
print "OK"
self['id'] = ''
2) Instance definition (your __init__ does not requires any explicit parameter, and for sure not the ancestor class)
project_class = Project() # ...and not here since this is an instance, not a Class
Comments
For Project to inherit from Base, you should not subclass it from object but from Base i.e class Project(Base). You get TypeError: init() takes exactly 1 argument (2 given) error when you instantiate Project class because the constructor takes only 1 parameter(self) and you pass base_class too. 'self' is passed implicitly by python.
class Project(Base)and not when instantiating it.