What are the naming rules of python language?

烟雨青岚
Release: 2020-06-10 16:05:13
Original
10458 people have browsed it

What are the naming rules of python language?

Python language naming rules:

1. Module:
Try to use lowercase names for modules, with the first letter Keep it lowercase and try not to use underscores (unless there are multiple words and a small number).

# 正确的模块名import decoderimport html_parser
# 不推荐的模块名import Decoder
Copy after login

2. Class name:
The class name uses CamelCase naming style, with the first letter capitalized. Private classes can start with an underscore.

class Farm():
    pass
class AnimalFarm(Farm):
    pass
class _PrivateFarm(Farm):
    pass
Copy after login

3. Function:
Function names must be lowercase. If there are multiple words, separate them with underscores.

def run():
    pass
def run_with_env():
    pass
Copy after login

Private functions add an underscore_ before the function.

class Person():
    def _private_func():
        pass
Copy after login

4. Variable name:
Variable names should be in lowercase letters. If there are multiple words, separate them with underscores.

if __name__ == '__main__':
    count = 0 
    school_name = ''
Copy after login

5. Constants:
Constants are named in uppercase letters separated by underscores.

MAX_OVERFLOW = 100
Class FooBar:
    def foo_bar(self, print_): 
        print(print_)
Copy after login

It is recommended not to use == for Boolean comparisons.

# Yes
if greeting::
   pass
# Noif greeting == True
   pass
if greeting is True: 
# Worse
  pass
Copy after login

Recommended tutorial: "python tutorial"

The above is the detailed content of What are the naming rules of python language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!