1: Simulation in Python uses COUT <<
Import sys
Class Ostream: DEF __INIT __ (Self, file): self.file = file
Def __lshift __ (self): self.file.write (STR (OBJ)); Return Self
Cout = ostream (sys.stdout) CERR = ostream (sys.stderr) endl = '/ n'
AGE = 23name = 'albert'
Cout << 'name:' << name << endl << 'agn:' << age << endl2. How to determine the caller in a function! ! (I just thought of, original oh) in a function
DEF FUN (): Pass
How do this function know who is called? It seems to be difficult in Class C language. But in Python is very simple
Import TRACEBACK
Def fun (): s = traceback.extract_stack () Print '% s invoked me!'% s [-2] [2]
This Fun function can know who calls it, and print it out, let's try:
>>> DEF A (): fun ()
>>> DEF B (): fun ()
>>> a () a invoked me! >>> b () b INVOKED ME! >>>
Traceback also has many other uses
OK! How to apply this feature? Play all the imagination. Interesting Usage 3: Hide and Display Windows Status Bars Using Windows API (using C SWIG Extension Python Example)
Use example:
>>> Import Hidewin >>> Hidewin.hide (1) # Hidden Status Bar 0 >>> Hidew.hide (0) # Display Status Bar 0 >>>
C language source program: hidewin.c
#include
int hide (int flag) {HWND hShellTray = FindWindow ( "Shell_TrayWnd", NULL); if (flag == 1) {ShowWindow (hShellTray, SW_HIDE);} else {ShowWindow (hShellTray, SW_SHOW);} return 0;}
SWIG configuration file hidwin.i
% module hidwin% {%}
Extern Int Hide (int FLAG);
Compilation and installation steps: 1. Install SWIG (see other documents)
2. Swig.exe -Python Hidewin.i (generate two files for hidwin_wrap.c hidwin.py)
3. Establish Win32 DLL project in VC, join hidwin_wrap.c, and hidwin.c, set the python header file, and library file directory 4. Compilation generation _Hidewin.dll
OK, you can perform the Python interpreter in this directory, use the example above or put the hidwin.py _hidewin.dll two files in the LIB directory of the Python installation directory, it is OK.
------------------------------------------ Interesting Use 4: Read a character String, and execute this is a powerful location than the C type language: program can generate a program!
Simple example:
Import parrsrc = "" "DEF FACT (N): if n == 1: Return 1 Return N * Fact (n - 1)" "" AST = Parser.suite (src) Eval (Ast.Compile ())
Print Fact (5)
---------------------------- FACT function is placed in a string SRC, generated by AST = Parser.suite (SRC) A AST object (specifically see the Python Random Document Library Refference 18eval (Ast.Compile ()) executed this AST object, so the FACT function is declared
We can also get source code from text files or networks. Dynamic execution through AST during execution of Python programs, this feature gives software development very flexible flexibility (of course, security needs to be considered) ------- ----------------------------------- Interesting 5: Mixin Technology (thanks to Limdou Introduction) DEF Mixin Pyclass, MixInClass: Print "Mix Class:", MixInclass, "INTO:", Pyclass, '/ n' pyclass .__ Bases__ = (MixInClass,)
Class A: Def __init __ (self): self.name = "Class A" Def Fun (Self): Print Self.name
Class B: Def __init __ (self): self.name = "Class B" DEF Add (Self, A, B): Print 'Function Defined In B' Return A B
Obj_a = a ()
Print Obj_aprint Dir (Obj_a), '/ N'
Mixin (A, B)
Print Obj_aprint Dir (Obj_a), '/ N'
Print obj_a.add (3,5)
----------------------------------------->Results of the:
>>> <__ main __. a instance at 0x00bb7f80> [__ doc__ ',' __init__ ',' __module__ ',' Fun ',' Name ']
Mix class: __main __. B INTO: __MAIN __. A
<__ main __. a instance at 0x00bb7f80> ['__ DOC__', '__init__', '__module__', 'add', 'fun', 'name']
Function defined in b8
Explain that MIXIN technology is the base class that makes a class into another class, which will have a new feature of the class of Mixin. In an example program, we entered the Class B Mixin, become a base class of A, so, the example of Class A has a method of class B (Add)
Obj_a = a () obj_a is an instance of class a Print Obj_a <__ main __. a instance at 0x00bb7f80> print dir (obj_a), '/ n' ['__doc__', '__init__', '__module__', 'fun', ' Name ']
Mixin (A, B) will be b mixin into A
Print Obj_a <__ main __. a instance at 0x00bb7f80>
Print Dir (obj_a), '/ n' ['__doc__', '__init__', '__module__', 'add', 'fun', 'Name'] Note, this time, there is more ADD method (defined in class B) )
Print Obj_a.add (3,5) Now A's instance can use the method in B.