Python code indentation and test module examples

Source: Internet
Author: User
This article mainly introduces the Python code indentation and test module Examples of detailed, with a certain reference value, now share to everyone, the need for friends can refer to

Objective

Python code indentation and test module is an essential part of learning Python, this article mainly introduces the Python code indentation and test module related content, share it for everyone to reference the study, the following words do not say more, come together to see the detailed introduction bar.

First, the Python code indentation

The Python function does not have a distinct begin and end, and does not indicate the opening and closing curly braces of the function. The only delimiter is a colon (:), and then the code itself is indented.

Example: Indent buil dcon necti onstr ing function

def buildconnectionstring (params): "" "Build a connection string from a dictionary of parameters. Returns string. "" " Return ";". Join (["%s=%s"% (k, v) for K, V in Params.items ()])

Code blocks are defined by their indentation. What I mean by "code blocks" is: functions, if statements, for loops, while loops, and so on. Starts indenting the beginning of the block, and cancels the indentation to indicate the end of the block. There is no obvious parenthesis, curly brace, or keyword. This means that the gap is important and consistent. In this example, the function code (including the doc string) indents 4 spaces. Not necessarily 4, as long as the same can be. The first line without indentation is considered to be outside the body of the function.

For example, an If statement shows an example of an if statement indentation.

def fib (n): (1) print ' n = ', n (2) if n > 1: (3) return n * FIB (n-1) Else: (4) print ' End of the line ' return 1

(1) This is a function called FIB, with a parameter n. All the code inside the function is indented.

(2) It is very easy to output content to the screen in Python, just use print. The print statement can accept any data type, including strings, integers, and other types, such as dictionaries and lists. Can even be mixed together to output, just separated by commas. All values are output to the same line, separated by spaces (commas are not printed). So when the fib is called with 5来, "n = 5" is output.

(3) If statement is a block of code. If the IF expression evaluates to True, the indented block immediately following it is executed, otherwise it goes into the else block.

(4) Of course if and else blocks can contain many rows, as long as they are equally indented. There are two lines of code in this else block. There is no special syntax for multi-line blocks of code, as long as indentation is done. After some initial protests and a few analogies with Fortran, you will be quietly
Treat the code indentation and begin to see its benefits. One of the main benefits is that all Python programs look the same, because indenting is a language requirement rather than a style. This makes it much easier to read and understand the Python code of others.

Note:python vs. Java: statement and statement splitting

Python uses hard returns to split the statement, Colon, and indentation to split the code block. C + + and Java use semicolons to split statements and curly braces to split blocks of code.

Second, the Python test module

All Python modules are objects and have several useful properties. You can use these properties to easily test the modules you write. Here is a tip using the IF name.

if name == "__main__":

There are several important observations before you continue to learn something new. First, the IF expression does not need to be enclosed in parentheses. Second, the IF statement ends with a colon, followed by an indent code.

Note:python vs. C: Comparison and assignment

Like C, Python uses = = to make comparisons, using = to do assignments. Unlike C, Python does not support inline assignment, so there is no case where you want to compare and unexpectedly assign a value.

So why is it a trick to say this particular if statement? The module is an object, and all modules have a built-in property name. The value of the name of a module depends on how you apply the module. If the import module, then the value of name is usually the file name of the module, without the path or file extension. But you can also run the module directly like a standard program, in which case the value of name will be a special default value, Main.

>>> import odbchelper>>> odbchelper.__name__ ' Odbchelper '

As long as you understand this, you can design a test suite for your module within the module to include the IF statement. When you run the module directly, the value of name is main, so the test suite executes. When you import a module, the value of name is something else, so the test suite is ignored. This makes it much easier to develop and debug a new module before it is integrated into a large program.

Tip:mac on OS if name __

On Macpython, an additional step is required to make the IF name technique effective. Click on the black triangle in the upper right corner of the window to pop up the module's Properties menu and confirm that Run as main is selected.

    • Python Unit Test Framework unittest how to use it

    • Python Test Implementation method

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.