zoukankan      html  css  js  c++  java
  • python 规范

    摘自google.

    https://i.cnblogs.com/PostDone.aspx?postid=9753605&actiontip=%E4%BF%9D%E5%AD%98%E4%BF%AE%E6%94%B9%E6%88%90%E5%8A%9F

    1、缩进

        Tip

        用4个空格来缩进代码

    2、代码太长:      

      如果一个文本字符串在一行放不下, 可以使用圆括号来实现隐式行连接:

      x = ('This will build a very long long '
         'long long long long long long string')
    或者:

      print \ 

       "hello world!"

    3、类:

    class SampleClass(object):

    4、注释:

        

    """Explicitly inherits from another class already."""
    
    或者
    
    #Explicitly inherits from another class already.

    5、字符串:

         通常可以用+,%,join(), format 三个函数进行处理

        %s   #字符串

        %d  #数字

    Yes: x = a + b
         x = '%s, %s!' % (imperative, expletive)
         x = '{}, {}!'.format(imperative, expletive)
         x = 'name: %s; score: %d' % (name, n)
         x = 'name: {}; score: {}'.format(name, n)
    Join:
    items = ['ni','hao'];
      employee_table = ''.join(items);

    6、处理文件:

       

    1、with open("hello.txt") as hello_file:
        for line in hello_file:
            print line



    2、import contextlib
    
    with contextlib.closing(urllib.urlopen("http://www.python.org/")) as front_page:
        for line in front_page:
            print line
     

    7、变量等命名规范:

      

    Python之父Guido推荐的规范
    
    Type                  Public                      Internal
    Modules                lower_with_under                 _lower_with_under
    Packages                lower_with_under                 ---------
    Classes                 CapWords                     _CapWords
    Exceptions               CapWords                     --------
    Functions               lower_with_under()               _lower_with_under()
    Global/Class Constants        CAPS_WITH_UNDER                 _CAPS_WITH_UNDER
    Global/Class Variables        lower_with_under                _lower_with_under
    Instance Variables          lower_with_under                _lower_with_under (protected) or __lower_with_under (private)
    Method Names              lower_with_under()              _lower_with_under() (protected) or __lower_with_under() (private)
    Function/Method Parameters      lower_with_under     
    Local Variables            lower_with_under
  • 相关阅读:
    01_javaSE面试题:自增变量
    SpringBoot(二十)_404返回统一异常处理结果
    MD5加密工具代码
    SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用
    读取本地文件转化成MultipartFile
    remote: http basic: access denied fatal: authentication failed for '‘解决办法
    git报错_you are not allowed to push code to protected branches on this project
    SpringBoot(十八)_springboot打成war包部署
    sql优化问题笔记(mysql)
    gitbook 简单使用
  • 原文地址:https://www.cnblogs.com/cbugs/p/9753897.html
Copyright © 2011-2022 走看看