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
  • 相关阅读:
    krpano--控制热点跳转到场景的指定视角
    bzoj 4237: 稻草人 -- CDQ分治
    bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演
    bzoj 3545/3551: [ONTAK2010]Peaks -- 主席树,最小生成树,倍增
    bzoj 4627: [BeiJing2016]回转寿司 -- 权值线段树
    bzoj 1901: Zju2112 Dynamic Rankings -- 主席树,树状数组,哈希
    bzoj 3252: 攻略 -- 长链剖分+贪心
    bzoj 5055: 膜法师 -- 树状数组
    bzoj 1006: [HNOI2008]神奇的国度 -- 弦图(最大势算法)
    bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
  • 原文地址:https://www.cnblogs.com/cbugs/p/9753897.html
Copyright © 2011-2022 走看看