首页 > 编程语言 >Python基础概念以及命名规范

Python基础概念以及命名规范

时间:2023-08-15 15:24:15浏览次数:47  
标签:power Python 0.0 规范 number int two 命名

Python

Basic

Introduction 介绍

Python is a dynamic and strongly typed programming language. It employs both duck typing and gradual typing via type hints.

While Python supports many different programming styles, internally everything in Python is an object. This includes numbers, strings, lists, and even functions.

Python是一个动态和强类型编程语言;

Name Assignment(Variables & Constants) 命名规范

Constant 常量

Constants are names meant to be assigned only once in a program. They should be defined at a module (file) level, and are typically visible to all functions and classes in the program. Using SCREAMING_SNAKE_CASE signals that the name should not be re-assigned, or its value mutated.

常量需要能够被明显辨认出;

Comment 注释

Comments in Python start with a # that is not part of a string, and end at line termination. Unlike many other programming languages, Python does not support multi-line comment marks. Each line of a comment block must start with the # character.

  1. 注释使用一个#
  2. 只能注释单行

Docstrings 文档字符串(文档注释)

# An example from PEP257 of a multi-line docstring.
def complex(real=0.0, imag=0.0):
    """Form a complex number.

    Keyword arguments:
    real -- the real part (default 0.0)
    imag -- the imaginary part (default 0.0)
    """

    if imag == 0.0 and real == 0.0:
        return complex_zero

文档字符串使用三重双引号(""")声明,并与代码块相同的缩进级别。

Docstrings are read by automated documentation tools and are returned by calling the special attribute .__doc__ on the function, method, or class name. Docstring conventions are laid out in PEP257.

Docstrings can also function as lightweight unit tests, which will be covered in a later exercise.

# An example on a user-defined function.
>>> def number_to_the_power_of(number_one, number_two):
        """Raise a number to an arbitrary power.

        :param number_one: int the base number.
        :param number_two: int the power to raise the base number to.
        :return: int - number raised to power of second number

        Takes number_one and raises it to the power of number_two, returning the result.
        """

        return number_one ** number_two
...

# Calling the .__doc__ attribute of the function and printing the result.
>>> print(number_to_the_power_of.__doc__)
Raise a number to an arbitrary power.

    :param number_one: int the base number.
    :param number_two: int the power to raise the base number to.
    :return: int - number raised to power of second number

    Takes number_one and raises it to the power of number_two, returning the result.

文档字符串可以被调用,通过 方法名.__doc__

标签:power,Python,0.0,规范,number,int,two,命名
From: https://www.cnblogs.com/CalvinZhao-Blog/p/17631364.html

相关文章

  • ​python爬虫——爬取天气预报信息
    在本文中,我们将学习如何使用代理IP爬取天气预报信息。我们将使用Python编写程序,并使用requests和BeautifulSoup库来获取和解析HTML。此外,我们还将使用代理服务器来隐藏我们的IP地址,以避免被目标网站封禁。1.安装必须的库首先,我们需要安装必须的库,包括requests、beauti......
  • 离线安装Python第三方库及依赖包
    1、问题在工作中经常需要在内网环境中安装python第三方库,使用从pypi上下载的whl文件来安装又经常遇到该库也需要依赖包,以至于并不能成功安装。2、解决办法(1)查看所需第三方库安装是否需要依赖库(以requests为例)pipshowrequests(2)使用命令将库及依赖包下载到本地(以requests为......
  • ​python爬虫——爬取天气预报信息
    在本文中,我们将学习如何使用代理IP爬取天气预报信息。我们将使用Python编写程序,并使用requests和BeautifulSoup库来获取和解析HTML。此外,我们还将使用代理服务器来隐藏我们的IP地址,以避免被目标网站封禁。1.安装必须的库首先,我们需要安装必须的库,包括requests、beaut......
  • python重采样tif影像,自定义空间分辨率
    importwarningsimportnetCDF4warnings.filterwarnings('ignore')warnings.filterwarnings('ignore',category=DeprecationWarning)importnetCDF4importpandasaspdimportnumpyasnpfromosgeoimportgdalimportmatplotlib.pyplotasp......
  • 某公司笔试题 - 密码验证合格程序(附python代码)
    #密码要求#1.长度超过8位;2.包括大小写字母,数字,其它符号,以上四种至少三种;3.不能有长度大于2的包含公共元素的字串重复(其他符号不含空格或换行)#数据范围:输入的字符串长度满足1<=n<=100#检测输入密码defcheckpassword(psw):iflen(psw)<=8orlen(psw)>100:r......
  • 数据库设计规范
    数据库设计(DatabaseDesign)是指对于一个给定的应用环境,构造最优的数据库模式,建立数据库及其应用系统,使之能够有效地存储数据,满足各种用户的应用需求(信息要求和处理要求)。一、数据库设计的原则表设计原则1(1)规范化与反规范化规范化的优点是减少了数据冗余,节约了存储空间,相应逻辑和......
  • python 面向对象 继承
     https://cloud.tencent.com/developer/article/1915788修改代码以解决报错classClass_1():  var_1=1    def__init__(self):    self.var_2=var_1classClass_2(Class_1):    def__init__(self):    self.var_4=self.var......
  • Python运算符全解析:技巧与案例探究
    在Python编程中,运算符是强大的工具,能够使我们在数据处理和逻辑判断方面更加灵活。本篇博客将全面探讨Python中常用的运算符,包括算术、比较、逻辑、赋值、位、成员和身份运算符,通过实际案例为你展示如何妙用运算符解决问题。算术运算符Python提供了一系列用于数值运算的算术运算符,如......
  • Python中对文件进行操作
    对于Python来说,文件处理绝对是一个常见的处理,读取文件、写入文件、生成文件……文件操作贯穿python变成始终。本篇文章将总结一下在平时编程过程中,常用的文件操作。以下将按照增删改查的顺序,对文件以及目录操作进行总结。新建文件和目录importos#新建文件new_file_path......
  • python 项目部署相关
    git代码管理和同步git本地上传全局配置gitconfig--globaluser.name"aaa"gitconfig--globaluser.email"[email protected]"进入项目目录初始化gitinit配置远程地址gitremoteaddoriginhttps://gitee.com/wupeiqi/xxxxx.git本地版本提交gitadd.g......