Static Invocation
"" " Example square, if desired the presence of the non-class members in the class, the method may be used @staticmethod called statically " "" class Joker (Object): DEF the __init__ (Self, A, B, C, D): Self II.A = A self.b, = B self.c = C self.d = D @staticmethod DEF is_valid (A, B, C, D): for I in [B, C, D]: IF I =! A: return False the else : return True DEF Area (Self): if res == True: area_ = self.a * self.b return area_
#输出的方法一 joker = Joker(2,2,2,2) res = joker.is_valid(2,2,2,2) if res == True: print(joker.area())
Method # two output
DEF Amin ():
Joker Joker = (2,2,2,2)
RES = joker.is_valid (2,2,2,2)
IF RES == True:
Print (joker.area ())
amin ()
inherit
""" 继承 """ class A(object): def __init__(self): self.a = 100 self.a1 = 200 def sum_(self): SUM_ = self.a + self.a1 return SUM_ class B(A): def __init__(self): #这个init是B自身的 A.__init__(self) def print(self): res = self.sum_() print(res) b = B() b.print()
"" " Inherited with arguments " "" class A (Object): DEF the __init__ (Self, A1): self.a = 100 self.a1 = A1 DEF sum_ (Self): sum_ = self.a + self.a1 return sum_ class B (a): DEF the __init__ (self, A1, B1): # this is the init B itself self.b1 = B1 A. the __init__ (self, A1) DEF Print (self): RES = self.sum_ () print(res) b = B(1000,'1') b.print()
Decorator
"" " Decorator Usage " "" DEF Number (FUNC): DEF Warp (A, B, C): NUM = A + B return FUNC (A, NUM, C) return Warp @number DEF SUM (N1, N2 , N3): # SUM here is equivalent to the above FUNC # where n, n2, n3 in fact equivalent to the above a, B, C # in fact, the last three parameters are the operational return func (a, num, c ) of a, NUM, C Print (N1, N2, N3) Print (N1 * N2) SUM ( 1, 2,4)
# Decorator DEF Joker (FUNC): DEF Warp (N1, N2, N3): NUM = N1 + N2 return FUNC (0, NUM, N3) return Warp # decorator summing the first two digits, the third function itself multiplied by the parameter and * @Joker DEF SUM (num1, num2, num3): Print (num1, num2, num3) Print (num2 * num3) SUM ( 10,2,3)
List
List of Formula
Advantages: fast calculation, as have all-time loaded into memory, the data volume is not too large for the case 10000-2000
Disadvantages: take up memory
List Builder
Advantages: saving memory space
Disadvantages: calculated slow, due to the generation
# List formula A = (X for X in Range (100000000000) IF X% 2 == 0) for I in Range (100 ): Print (Next (A)) "" " listing formula # listing formula a = [x for x in range (100000000000 ) if x% 2 == 0] advantages: fast calculation, as have all-time loaded into memory, the data volume is not too large for the case 10000-2000- disadvantages: memory for generator # a = (x for x in range (100000000000) if x% 2 == 0) advantages: saving memory disadvantages: slow calculation as to generate a "" " Import OS path = ' / the Users / Joker / Jokers / the DataSet / Stanford-Dogs-DataSet / the Annotation ' res = os.listdir(path) print(res) genter = (dir_ for dir_ in res) print(next(genter))