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))
任何机构都能解释一下这段代码吗?实际上它做了什么...
答案 0 :(得分: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"""