Python tips (3)

Source: Internet
Author: User

1. Use the pywinauto automation framework to implement the Failover program.

import win32com.clientfrom pywinauto import Applicationfrom time import sleep, timeapps = {'iexplore':'IEFrame',        'notepad':'Notepad'}lst = []for key:value in apps.items():    key = key + '_'    key = Application.Connect(class_name = value)    lst.append(key[value])    #print key    #print key(value)for i in range(2):    for obj in lst:        obj.TypeKeys("{TAB}")        sleep(.8)

2. When using Python itself: __name _ = _ main __

When called: __name _ = File Name (excluding suffix)

3. When multiple values are assigned to Python, the values on the right are calculated before the values are assigned.

4. The for loop in python can contain an else clause. When the loop ends normally, its content is executed. However, if a break statement exists, its content is not executed. The following program outputs a prime number of less than 10.

for n in range(2, 10):    for x in range(2, n):        if n % x == 0:            print n, 'equals', x, 'x', n/x    else:        print n, 'is a prime number'

Output result:

>>> 2 is a prime number3 is a prime number4 equals 2 x 24 is a prime number5 is a prime number6 equals 2 x 36 equals 3 x 26 is a prime number7 is a prime number8 equals 2 x 48 equals 4 x 28 is a prime number9 equals 3 x 39 is a prime number>>> 

5. ''' contains docstring. It can be printed with print. As follows.

def M():    '''test docstring'''    returnprint M.__doc__

Output result:

>>> test docstring>>> 

6. if the python function has a ** name parameter in the form parameter table, this form parameter can receive a dictionary when called. The dictionary contains all key parameters that do not match any of the form parameters, A special parameter named * name can also be used in the form parameter table, which accepts a sequence table composed of all the location parameters that cannot match, * name can only appear before ** name (* name is similar to * ARGs, ** name is similar to ** kW), and can be set from 0 to multiple normal parameters before any number of parameters.

 

7. Lambda statements are used to create new function objects and return them at runtime. As follows.

def make_repeater(n):    return lambda s:s*ntwice = make_repeater(2)print twice('word')print twice(5)>>> wordword10>>> 

Here, the make_repeater function is used to create a new function object at runtime and return it. Lambda statements are used to create function objects. In essence, Lambda requires a parameter, followed by a single expression as the function body, and the value of the expression is returned by the newly created function.

Note: even print statements cannot be used in Lambda, but expressions are only allowed.

 

8. list []

Sequence Table tuple

Tuples ()-> once generated, cannot be changed

Dictionary {}

The comparison of objects of different types in python is also valid at present, and the results are definite but meaningless. Different types are sorted by type names. Therefore, list) it is always smaller than the string, and the string is always smaller than the tuple. However, the program cannot rely on such comparison rules, and the language implementation may change. Different numerical types can be compared by numerical values, so 0 equals 0.0, and so on.

 

9. when the format "from package import item" is used, the imported item can be a sub-module (or sub-package) of the package ), it can also be other names defined in the package, such as functions, classes, and variables. The Import Statement first checks whether the required items are defined in the package. If not, it is assumed to be a module and then imported. If not, importerror is triggered.

Conversely, when "import item. subitem. in the format of subsubitem, except for the last package or module, it cannot be the class, function, or variable defined in the previous item.

 

10. pickle is a standard method used to save a python object and call it when it is run again by another program or the same program. This is called a persistent object ".

 

11. Standard exception names in Python are built-in Identifiers (not reserved keywords ).

 

12. In python, try (RAISE) catch t finally differs from try (throw) catch finally in C.

In python, the finally clause is executed no matter whether the try clause has exceptions. When an exception occurs, the finally clause is executed first and then the exception is thrown again, when the try statement exits with the break or return statement, the finally clause is also executed.

Note that the try statement cannot have both the limit t clause and the finally clause, and can be nested if necessary.

 

13. Small error in Python: the private variables of the base class can be used to export the class and the base class with the same name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.