First, static methods and class methods
1. static call: @staticmethod
class A(object): @staticmethod def a(): print('a') A.a()
If you need presence of non-class members of the class, you can use the static method call
2. Example
It is determined whether an input is the four sides of the square, if it is: calculating the area, with @staticmethod
class zfx(object): def __init__(self,a,b,c,d): self.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 else: return True def area(self): if res == True: area_ = self.a *self.b return area_ fx = zfx(5,5,5,5) res = fx.is_valid(5,5,5,5) fx.area() if res == True: print(fx.area())
3. Class Methods
(1) Default:
def A(bl = 'hello'): print(bl) A()
The output is: hello
(2)cocaltime
# Localtime call the library: from Import Time Time, localtime, SLEEP Print (Time ())
Time stamp, from January 1, 1970 00.00.00 number of seconds to start now experienced
(3)@classmethod
@classmethod: Get Class itself (cls) attributes, and can be changed.
a classmethod modifiers need not instantiate the corresponding function, no self parameters, but the first parameter is a required parameter itself cls class can be invoked attribute class, the class methods, and other objects instantiated
Relationship (inherited) between the Class 4 Class
(1) The first step is to write the name of the class inherited class
(2) the class name suggested (classname) __ init __ (self): a .__ init __ (self)
(3) Example: Create a parent class to create a subclass, the parent and two numbers calculated sum_, print this subclass sum_
class fu(object): def __init__(self): self.a = 10 self.b = 20 def sum_(self): sum=self.a + self.b return sum class zi(fu): def __init__(self): fu.__init__(self) def PRINT(self): res = self.sum_() print(res) b= zi() b.PRINT()
Output: 30
The list and the list production unit formula
# List formula A = [X for X in Range (100)] Print (A) # list generator A = (X for X in Range (100)) Print (A)
List Builder List Builder type with advantages and disadvantages:
(1) formula:
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
(2) Generator:
Advantages: save memory
disadvantages: slow calculation as to generate
Second, the decorator
1. decorator usage
Go walk decorator function
Example 2. decorator: create three functions, two parameters, the decorator and the processing of these two parameters, each of the print function of these two parameters
Joker DEF (FUNC): DEF Warp (N1, N2, N3): NUM = N1 + N2 return FUNC (0, NUM, N3) return Warp * decorator summing the first two digits, the first function itself multiplied by the three parameters and * @Joker DEF SUM (num1, num2, num3): Print (num1, num2, num3) Print (num2 * num3) SUM (10,2,3)