Python code indentation and test modules

1. Python code indentation

 

Python functions have no obvious begin and end , and no curly braces to mark the beginning and end of the function. The only delimiter is a colon ( : ), and the code itself is indented.

For example: indent buil dCon necti onString 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. By "block of code" I mean: functions, if statements, for loops, while loops, etc. Begin indenting indicates the beginning of a block, and unindenting indicates the end of a block. There are no obvious parentheses, braces or keywords. This means whitespace is important and consistent. In this example, the function code (including the doc string ) is indented by 4 spaces. It doesn't have to be 4, as long as they are consistent. The first line without indentation is considered outside the function body.

For example: "if statement" shows an example of indentation of an if statement.
 
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 one parameter n . All code inside a function is indented.
 
(2) It is very easy to output 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. You can even mix the output together, just separate them with commas. All values ​​are output on the same line, separated by spaces (commas are not printed). So when fib is called with 5, it will output "n = 5".
 
(3) The if statement is a type of code block. If the if expression evaluates to true, the following indented block will be executed, otherwise the else block will be executed.
 
(4) Of course if and else blocks can contain many lines, as long as they are all equally indented. There are two lines of code in this else block. There is no other special syntax for multi-line code blocks, just indentation. After some initial protests and a few mocking analogies with Fortran, you'll be at peace
Treat code indentation and start seeing its benefits. A major benefit is that all Python programs look the same, because indentation is a language requirement rather than a style. This makes it much easier to read and understand other people's Python code.
 
Note: Python vs. Java: Statements and Statement Splitting
Python uses hard returns to separate statements, and colons and indents to separate blocks of code. C++ and Java use semicolons to separate statements, and curly braces to separate blocks of code.


2. 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's a trick using if __name__ .

if __name__ == "__main__":
 
Before moving on to learn something new, there are several important observations. First, if expressions don't need to be enclosed in parentheses. Second, an if statement ends with a colon, followed by indented code.
 
Note: Python vs. C: Comparison and Assignment
Like C, Python uses == for comparison and = for assignment. Unlike C, Python does not support inline assignment, so there is no chance of accidentally assigning a comparison when you want to do it.
 
So why is this particular if statement a trick? Modules are objects, and all modules have a built-in attribute __name__ . The value of a module's __name__ depends on how you apply the module. If importing a module, the value of __name__ is usually the module's filename, without a 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, __main__ .
 
>>> import odbchelper
>>> odbchelper.__name__
'odbchelper'

  

Knowing this, you can design a test suite for your module inside the module that includes this if statement. When you run the module directly, the value of __name__ is __main__ , so the test suite executes. When you import the module, the value of __name__ is something else, so the test suite is ignored. This makes it much easier to develop and debug new modules before integrating them into a larger program.
 
Tip: if _ _ name __ on Mac OS
On MacPython, an extra step is required to make the if __name__ trick work. Click the black triangle in the upper right corner of the window to pop up the properties menu of the module and confirm that Run as __main__ is selected.
 

Reference: Dive Into Python 
Thanks for reading the Shanghai Shangxuetang python article, for more, please click  Shanghai python training

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325282250&siteId=291194637