Importing user defined modules in Python
Question: I have the following module definition for a Stack:

    
    
    class Stack:
     def __init__(self):
         self.items = []
    
     def isEmpty(self):
         return self.items == []
    
     def push(self, item):
         self.items.append(item)
    
     def pop(self):
         return self.items.pop()
    
     def peek(self):
         return self.items[len(self.items)-1]
    
     def size(self):
         return len(self.items)
    

I am trying to import this module in another program as follows:

    
    
    import Stack
    s=Stack()
    
    print(s.isEmpty())
    s.push(4)
    s.push('dog')
    print(s.peek())
    

I get the following error:

    
    
    s=Stack()
    TypeError: 'module' object is not callable
    

Have I imported incorrectly? The Stack class is within the same folder as the
class that is implementing it. Or have I called the constructor incorrectly?


Answer: You should use:

    
    
    from Stack import Stack
    

In 's=Stack()', the 'Stack' is a module name.

