首页 > 代码库 > 定制类

定制类

class Fib(object):
    def __init__(self):
        self.a,self.b=0,1
    def __iter__(self):
        return self
    def __next__(self):
        self.a,self.b=self.b,self.a+self.b
        if self.a>100:
            raise StopIteration()
        return self.a
    def __getitem__(self,n):
        if isinstance(n,int):
            a,b=1,1
            for x in range(n):
                a,b=b,a+b
            return a
        if isinstance(n,slice):
            start=n.start
            stop=n.stop
            step=1
            step=n.step
            a,b=1,1
            R=[]
            K=[]
            for x in range(stop):
                if x>=start:
                    R.append(a)
                a,b=b,a+b
            K=R[::step] 
            return K
print(Fib()[0:99:2])                        

  

 

 

 

class Mydict(object):
    def __init__(self,key,value):
        self.dict={}
        self.dict[key]=value
    def __getitem__(self,key):
        return self.dict[key]
    def __setitem__(self,key,value):
        self.dict[key]=value
    def __delitem__(self,key):
        self.dict.pop(key)
a=Mydict(‘Eng‘,99)
a[‘Math‘]=90
print(a[‘Eng‘])
print(a[‘Math‘])
del a[‘Math‘]
print(a[‘Math‘])

  

  

 

定制类