-4
class stack():
     def __init__(self):
          self.items=[]
     def push (self,item):
          self.items.append(item)
     def pop (self):
          return self.items.pop()
     def length(self):
          return (len(self.items))

任何人都可以向我解释这段代码吗?其实它的作用...

4

1 回答 1

0

创建对象时,例如:

obj=stack()

您可以访问该类中的定义,例如:

obj.push(3)
"""adds number 3 to your stack"""

obj.pop()
"""returns last item from your stack that was inserted into it and deletes it from the stack (in this case 3)"""

obj.length()
"""returns length of your stack, now it is 0 but if we didn't perform obj.pop() that would be 1"""
于 2013-06-15T16:59:05.893 回答