Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python programming style     - Based on Python: OpenCV simple image manipulation (Programming)

- VMware virtual machine to install CentOS 7 (Linux)

- Oracle database, some basic grammatical structures (Database)

- Experience PHP 7.0 on CentOS 7.x / Fedora 21 (Server)

- Install Web-based monitoring tool: Linux-Dash (Server)

- When RHEL7 use fdisk partition, all partitions can not be used (Linux)

- Effect MongoDB plan cache (Database)

- RMAN parameters of ARCHIVELOG DELETION (Database)

- Linux firewall to prevent external network attacks (Linux)

- Linux dd command make U disk boot disk (Linux)

- Hive handle count distinct inclination to produce data processing (Database)

- Distributed Hadoop1.2.1 cluster installation (Server)

- Teach you how to synchronize Microsoft OneDrive in Linux (Linux)

- To compiler and install MariaDB-10.0.20 under CentOS 6.6 (Database)

- When Linux Detailed time zone and common function of time (Linux)

- Linux / proc directory Comments (Linux)

- Ubuntu 14.10 Apache installation and configuration (Server)

- About AWR More Description (Database)

- Nginx request processing (Server)

- Linux how to handle file names that contain spaces and special characters (Linux)

  Python programming style
  Add Date : 2017-08-26      
1, Python statements and syntax
2, Python identifier
3, the basic programming style

First, Python statements and grammar
1, can be anywhere from the beginning of a line
2, continued
 '' ': Closed operator, single statement across multiple lines

3, the code group
 , Indent the same set of statements to form a block of code
 , The first line to keywords, such as if, while, etc., to the end of the colon
 , Python uses indentation to separate the code group, the same code group of code must be strictly left-aligned, otherwise it will cause a syntax error
 , Placing multiple statements on the same line
;: Use a semicolon as a delimiter
 , The module
 Each Python script file can be treated as a module; the name of the module is also the script name
 The code in the module can be a direct implementation of the script, it can be similar to the code library functions can be implemented by other modules import (import) because the module will be the moment of import will be implemented; so the modules are generally stored Is some of the code can be called;

#! / Usr / bin / python2.6
# Coding = utf-8
Name = "Tom"

Def prinName (a):
    Print a
# Indent the same set of statements constitute a block of code; num with the previous code block has nothing to do
Num = 4
Print num
Print prinName (name)
# Import module is the need to specify the module directory path;
[Root @ Oracle ~] # cat mod.py
#! / Usr / bin / python2.6
Def prinName (a):
Print a
B = "Black"
Print a + b
[Root @ oracle ~] # cat import.py
#! / Usr / bin / python2.6
Import mod

Mod.prinName ()

Second, the identifier
1, the identifier is allowed in the computer language as a name of a valid string collection
 Some of these are keywords, which are language identifiers, and therefore reserved words, and can not be used for other purposes
Python also has a set of identifiers called "built-in", which are not recommended, although they are not reserved words.
2, Python identifier
 The first character can only use letters or underscores
 The remaining characters can use letters, numbers, or underscores
 Distinguish character case

Third, Python basic programming style
1, Notes
 Neither lack of comments, but also to avoid excessive comments
2, the document
Python allows document strings to be obtained dynamically via __doc__
3, indentation
 Uniform indentation of 4 characters
4, the identifier name
 See the name of knowledge

>>> str .__ doc__
'Str (object) -> string \ n \ nReturn a nice string representation of the object. \ NIf the argument is a string, the return value is the same object.'
>>> print str .__ doc__
Str (object) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object

Python naming convention
1, with a single underscore at the beginning of the variable name (_x) will not be imported from the module import * statement
2, before and after the underline variable name (__x__) is a system variable name, the interpreter has special significance
3, beginning with two underscores, but not underlined at the end of the variable name (__x) is the class of local variables
4, interactive mode, only a single underscore variable name (_) used to save the results of the last table expression

>>> a = "I love:"
>>> b = "pig"
>>> print a + b
I love: pig
>>> print a .__ add __ (b)
I love: pig

Class str (basestring)
| Str (object) -> string
| Return a nice string representation of the object.
| If the argument is a string, the return value is the same object.
| Method resolution order:
| Str
| Basestring
| Object
| Methods defined here:
| __add __ (...)
| X .__ add __ (y) < ==> x + y
| __contains __ (...)
| X .__ contains __ (y) < ==> y in x
| __eq __ (...)
| X .__ eq __ (y) < ==> x == y
| __format __ (...)
| S .__ format __ (format_spec) -> string
| __ge __ (...)
| X .__ ge __ (y) < ==> x> = y
| __getattribute __ (...)
| X .__ getattribute __ ( 'name') < ==> x.name
| __getitem __ (...)
| X .__ getitem __ (y) < ==> x [y]

Python file structure

#! / Usr / bin / python2.6 # (1) Starting line
# Coding = utf-8 #mod .__ doc__ Referencing class documentation
# "This is test module" (2) Module documentation (document string)
Import sys # (3) module import
Import os

Debug = true
Name = "Tom" # (4) global variables

Class FooClass (object): # (5) class definition (if any)
    "Foo class"

Def test ():
"Test function"
Foo = FooClass () # instantiate the class
If debug:
Print 'ran test ()'

Def prinName (a): # (6) Function definition (if any)
Print a
# Indent the same set of statements constitute a block of code; num with the previous code block has nothing to do
Num = 4
Print num
Print prinName (name)

If __name__ == '__main __' # (7) the main program
Test ()
PrinName ()

Python file main program
1, the main program
 This code is executed regardless of whether the current module is imported by another module or executed directly as a script
Note: All modules have the ability to execute code
 The highest level python statement (without indentation) is executed when the module is imported, whether or not it really needs to be executed
3, the appropriate approach: In addition to those who really need to implement the code, all the function codes are established through the function, so
 Only in the main program module to write a large number of top-level executable code
 There should only be less top-level execution code for the module being imported
4, __ name__ indicates how the module should be loaded Each module has a built-in variable named __name__, which changes depending on how this module is called
 If the module is imported, the value of __ name__ is the name of the module
 If the module is executed directly, the value of __ name__ is "__main__":
- Ubuntu study notes and related problem solving (Linux)
- MySQL can not write the data keyword conflicts (Database)
- Linux with Windows Explorer as a security system (Linux)
- To use iostat display I / O status under Linux (Linux)
- DRBD Principle and Features Overview (Server)
- OpenSSL to generate public and private key (Linux)
- The user of fedora is not in the sudoers file method to solve (Linux)
- The bulk batch multiple rows of data collect (Database)
- Linux Task Scheduler (at, crontab) (Linux)
- Ubuntu 14.04 users how to install VLC 2.2.0 (Linux)
- Oracle inline view updates problems encountered (Database)
- Linux platform Oracle MySQL connection (Database)
- Linux basic introductory tutorial ---- Software Installation under Linux (Linux)
- IOwait Linux system monitoring diagnostic tools (Linux)
- CentOS 6.5 platform offline compile and install PHP5.6.6 (Server)
- Linux garbled file delete method (Linux)
- Python Django direct implementation of sql statement (Programming)
- linux smartd [FAILED] appears at startup (Linux)
- Linux Getting Started tutorial: 3D effects and beautify your desktop (Linux)
- MariaDB phpMyAdmin installation and configuration issues to resolve under CentOS7 (Database)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.