程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Naming conventions for Python

編輯:Python

Naming conventions play an important role in writing code , Using naming conventions can make it more intuitive to understand what the code stands for , Make the code readable 、 Ease of writing and clarity .

Today I want to talk to you about Python Common naming and naming conventions .

Common nomenclature

Hump nomenclature (CamelCase)

Camel nomenclature (Camel-Case) The word comes from Perl A mixed case format commonly used in languages , It is also called hump nomenclature , It is a set of naming rules for computer programming ( practice ), There is no absoluteness or compulsion , To increase recognition and readability .

It's a mixture of uppercase and lowercase letters to form the names of variables and functions , It is divided into small hump nomenclature and large hump nomenclature

  • Little hump nomenclature (lowerCamelCase)

Except for the first word , All other words are capitalized .

  • The name of the great hump (CamelCase)

The great hump method ( Pascal nomenclature ) Words are capitalized .

Snake nomenclature (snake_case)

It's all made up of lowercase letters and underscores , Words are in lowercase and underlined , Also known as “ Underline nomenclature ”.

String nomenclature (kebab-case)

Underline the words “-” Connect , Also known as spine nomenclature (spinal-case)、train-case.

Hungarian nomenclature (HN-case)

The variable names are followed by the attributes 、 type 、 Describe the composition , Such as :int iUserName,i by int Type abbreviation ,UserName Is to describe .

Free nomenclature (studly caps)

Mixed case , Along with the gender ……

Python Naming rules

  • Variable

  • The variable name must be a valid identifier ;

  • Variable names do not use Python Reserved word in ;

  • Use small letters with caution l And capital letters O;

  • Use meaningful words as variables ;

  • Try to be lowercase , If there are more than one word , Separate with an underscore, that is, use the snake naming method (snake_case) name .

i = 0
if i>0:
number = 0,
school_name= " Tsinghua"

Constant

Constants are in uppercase , Such as multiple words , Separate... With underscores .

PAI=3.14
MAX_CONNECTION=100
CONNECTION_TIMEOUT=500

Function name

All lowercase when there is only one word , When there are more than one word, use the serpentine nomenclature (snake_case) name .

def function():
pass
def function_name():
pass

Private functions place an underline before the function .

classPerson():
def _private_func():
pass

Class name

The class name uses camel hump (CamelCase) Naming style , title case , Private classes can start with an underscore .

class ClassName():
pass

Module name

Snake nomenclature is recommended (snake_case).

impor module_name
import html_parser

Learning resource sharing

Finally, thank everyone who reads my article carefully , Watching the rise and attention of fans all the way , Reciprocity is always necessary , Although it's not very valuable , If you can use it, you can take it

These materials , For those who want to engage in 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! You can get it for free by joining our communication group below , I hope that's helpful …….


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved