博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一. python数据结构与算法
阅读量:4322 次
发布时间:2019-06-06

本文共 650 字,大约阅读时间需要 2 分钟。

class Bag(object):    def __init__(self,maxsize=10):        self.maxsize=maxsize        self._item=list()    def add(self,item):        if len(self)>self.maxsize:            raise Exception('bag is full')        self._item.append(item)    def remove(self,item):        self._item.remove(item)    def __len__(self):        return len(self._item)    def __iter__(self):        for item in self._item:            yield itemdef test_bag():    bag=Bag()    bag.add(1)    bag.add(2)    bag.add(3)    assert len(bag)==3    bag.remove(3)    assert len(bag)==2    for i in bag:        print(i)test_bag()

转载于:https://www.cnblogs.com/peitianwang/p/11435266.html

你可能感兴趣的文章
原型模式
查看>>
Hadoop RPC源码阅读-交互协议
查看>>
WASAPI、DirectSound/DS、WaveOut、Kernel Streaming/KS
查看>>
Perl按行分割文件
查看>>
根据现有表操作基于active record的model
查看>>
NotMapped属性特性
查看>>
Count and Say
查看>>
GridView数据导入Excel/Excel数据读入GridView
查看>>
566. Reshape the Matrix
查看>>
python数据结构与算法之搜索
查看>>
(最小点覆盖) poj 2226
查看>>
(树形DP) poj 3659
查看>>
获取类的属性名和值
查看>>
python对json的操作总结
查看>>
学习进度表第十一周
查看>>
js屏蔽回车键
查看>>
Memcached通用类(基于enyim.com Memcached Client)
查看>>
c#组元(Tuple)的使用
查看>>
【NO.66】转 Yahoo的军规条例
查看>>
vim基础学习之搜索功能
查看>>