python implement custom cut sheet

Import Numbers
 class Group:
     # support slicing 
    DEF  the __init__ (Self, GROUP_NAME, COMPANY_NAME, Staffs): 
        self.group_name = GROUP_NAME 
        self.company_name = COMPANY_NAME 
        self.staffs = Staffs 

    DEF  __reversed__ (Self): 
        self.staffs.reverse () 

    DEF  __getitem__ (Self, Item): # implements this magic function, you can be the next iteration of the underlying elements of some elements can get the class, you can also make it an iterable 
        CLS = of the type (Self) # use relative coding, the type acquisition
         IF isinstance (Item, Slice):
             returnCLS (GROUP_NAME = self.group_name, COMPANY_NAME = self.company_name, Staffs = self.staffs [Item])
         elif the isinstance (Item, numbers.Integral):
             return CLS (GROUP_NAME = self.group_name, COMPANY_NAME = self.company_name, Staffs = [self.staffs [Item]]) 

    DEF  the __len__ (Self):
         return len (self.staffs) 

    DEF  the __iter__ (Self): # magic implement the function, it can be an iterative class object
         return ITER (self.staffs ) 

    DEF  __contains__ (Self, Item):
         IF Item in self.staffs:
             return True
         the else :
            return False

staffs = ["bobby1", "imooc", "bobby2", "bobby3"]
group = Group(company_name="imooc", group_name="user", staffs=staffs)
print(group[:2])
reversed(group)
for user in group:
    print(user)

 

Guess you like

Origin www.cnblogs.com/callyblog/p/11333194.html