0
# 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)
1
  • 4
    You have to declare inheritence when defining the class (class Project(Base) and not when instantiating it. Commented Jan 8, 2014 at 20:21

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

When you're instantiating a class, you don't need to pass in base_class. That's done at definition. __init__ takes exactly 1 argument, which is self, and automatic. You just need to call

project_class = Project()

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.