Why i use len() of a deque is wrong?


  • 0
    Q

    class Queue(object):
    def init(self):
    """
    initialize your data structure here.
    """
    self.items = collections.deque()

    def push(self, x):
        """
        :type x: int
        :rtype: nothing
        """
        self.items.append(x)
    
    def pop(self):
        """
        :rtype: nothing
        """
        self.items.popleft()
    
    def peek(self):
        """
        :rtype: int
        """
        return self.items[0]
        
    
    def empty(self):
        """
        :rtype: bool
        """
        if self.items.__len__() == 0:
            return True
        else:
            return False
    

    I want to know why I can't use self.len(items) for the length of deque. It gave an error 'Queue' Object doesn't have the attribute 'len'. However, I saw this attribute in the python doc.


  • 0

    You can't use self.len because as you're told, it doesn't exist. Just use len.

    Btw, please format code properly. Especially Python code.


  • 0
    Q

    Thank you for your advice. When I paste my code here, it formats my code improperly. Sorry for the inconvenience.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.