Decorative Device
Definition: The essence is function, decorate other function is to add new function to it;
Principle: 1. Cannot modify the source code of the decorated function
2. Cannot modify the calling mode of the decorated function
Implement the Adorner Knowledge Reserve:
1. function is variable;
2. Higher-order functions
3. Nesting functions
High-price function + nested function = Adorner
Which of the following is the case I'm talking about
1 Import Time2 defTimmer (func):3 defWarpper (*args,**Kwargs):4Start_time=time.time ()5 func ()6Stop_time=time.time ()7 Print('The func run time is%s'% (stop_time-start_time))8 returnWarpper9 Ten @timmer One deftest1 (): ATime.sleep (3) - Print('In the test1') - theTest1 ()
And that *args, and **kwargs, is for when the function to be decorated has parameters, then the function that is called needs to be passed to be called. And, to pass the argument, we just need to be func (*args,**args). This is still recommended.
To see the silver Horn King's blog, but I did not read the first time read: http://www.cnblogs.com/wupeiqi/articles/4980620.html
JSON
Http://www.cnblogs.com/coser/archive/2011/12/14/2287739.html this podcast is well written, especially the object's piece.
Pickle
Pickle is similar to JSON, but it is only used in Python to conform to the specification. Unlike JSON, it's generic.
ImportPickledefSayhi (name):Print("Hello,", name) info= { 'name':'Alex', ' Age': 22, 'func': Sayhi}f= Open ("Test.text","WB")#Print (Json.dumps (info))Print() F.write (Pickle.dumps (info)) F.close ()Serialization of Pickle
ImportPickledefSayhi (name):Print("Hello2,", name) F= Open ("Test.text","RB") Data= Pickle.load (f)#data = Pickle.loads (F.read ())Print(data["func"]("Alex"))Pickle Deserialization
by Pickle.dumps () to serialize what you want to serialize, you can make the array a method, how to serialize it, and then use it to deserialize it. Look at the deserialization session, call the
Pickle.load (), then, electrophoresis the method of the value of a key in the dictionary, the function is the variable, do not suspect here is very unreasonable. JSON is not intended for this purpose, because JSON is not written specifically for Python.
Python decorator, Json,pickle