python语言有几种注释方式,python语言注释的语法是怎样的

  python语言有几种注释方式,python语言注释的语法是怎样的

  本文主要介绍Python类的基本编写和注释风格,有很好的参考价值。希望对大家有帮助。如有错误或不足之处,请不吝赐教。

  00-1010 Python类1的基本编写和注释风格。Python 2中的类。语言风格规范示例Python类的简单编写

  

目录

  Python是一种面向对象的语言。使用类抽象可以大大提高代码的重用性和结构性,减少重复造轮子的过程,使代码更加清晰,更容易理解和维护。

  

Python类基本写法与注释风格

  python中的类提供了一系列数据和方法的组合。类是python的一个对象,从它可以构建一个新的实例。实例包含类的属性和类中声明的方法。我们先来看一个基础类的写作:

  类别狗(对象):

  这是一个狗类的例子

  def __init__(self,name):

  这是初始功能

  self.name=name

  定义语音(自身):

  狗会像汪汪一样说话

  打印(汪汪汪)

  这是一个非常简单的类,但是它包含了类的几个重要部分,包括类的声明,初始化的构造函数,属性和成员方法的定义等等。

  有几点需要注意:object是python中所有类的基类,在类初始化时是显式继承的。

  是self类中的一个实例,该类本身就是实例。初始化后有一系列的属性和方法,类方法的第一个参数根据约定需要以self开头。

  完整类的声明还将包括基本属性、私有属性和受保护变量:

  类别狗(对象):

  这是一个狗类的例子

  Animal_kind=dog #基本属性

  Animal_legs=4 #基本属性也建议写入初始化构造函数。

  DEF _ _ init _ _(自身,姓名,年龄,参数.)3360 #是用__init__(self,params)初始化的

  这是初始功能

  self.name=name

  年龄=年龄

  #您还可以定义各种其他属性,并在初始化实例时为传递的参数赋值。

  自我。__gender=male #这两个下划线以私有内部属性开头,这些属性只能在类内部访问。

  def __privateGender(self):

  这是pravate方法

  打印(这只狗性别是%s ,自我。_ _性别)

  定义语音(自身):

  狗会像汪汪一样说话

  打印(汪汪汪)

  打印(自我。__privateGender(self))

  定义运行(自身):

  用腿跑

  print(这只狗有%d条腿可以跑 %self.animal_legs )

  #定义一大堆不同的方法

  类可以执行继承和方法重写,可以基于一个类继承,也可以基于多个类进行多重继承。

  哈士奇级(狗):

  哈士奇固有的狗的属性和方法

  def __init__(自我,姓名,年龄,颜色,参数):

  狗。__init__(self,name,age) #使用父类Dog的初始化

  self . color=color #子类中特定属性的初始化

  定义跳转(自身):

  哈士奇

   special jump function"""

   print(This dog could jump jump)

   def voice(self):

   """重写覆盖父类的函数,实现自己的特殊的方法"

   print(AoAoAoWu~~~~~~)

  

  

2.语言风格规范

  为了更好的便于阅读和复用代码,还需要使得代码满足一定的语言风格,这里选用了google的风格规范来对类进行声明,下面是一个例子

  

# ref from:https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/

  class MyDog(object):

   """Summary of class here. #1.首先一句话简短的总结这个类的功能和作,文档字符串需要用三引号包括

   # 对齐,空一行

   If the class has public attributes, they may be documented here

   in an ``Attributes`` section and follow the same formatting as a

   functions ``Args`` section. Alternatively, attributes may be documented

   inline with the attributes declaration (see __init__ method below).

   Properties created with the ``@property`` decorator should be documented

   in the propertys getter method.

   Longer class information.... #随后详细的说明类的细节

   Longer class information.... #类内部第一行的开始的文字都可以被__doc__

   # 空一行,开始写这个类的各个属性,包括数据类型和作用

   Attributes:

   likes_spam: A boolean indicating if we like SPAM or not. #属性的声明,包括数据类型和作用,xxx类型的数据for/used to/ofxxx

   eggs: An integer count of the eggs we have laid.

   """

   def __init__(self, likes_spam=False):

   """Inits SampleClass with blah."""

   # 下面是详细的例子

   """Example of docstring on the __init__ method.

   # 对于初始化方法的说明

   The __init__ method may be documented in either the class level

   docstring, or as a docstring on the __init__ method itself.

   Either form is acceptable, but the two should not be mixed. Choose one

   convention to document the __init__ method and be consistent with it.

   # 对于初始化方法的一些记录

   Note:

   Do not include the `self` parameter in the ``Args`` section.

   # 初始化的参数输入,对于方法来说参数名(数据类型):描述的格式来写

   Args:

   param1 (str): Description of `param1`.

   param2 (:obj:`int`, optional): Description of `param2`. Multiple

   lines are supported.

   param3 (:obj:`list` of :obj:`str`): Description of `param3`.

   """

   self.likes_spam = likes_spam

   self.eggs = 0

   # 输入参数的初始化

   self.attr1 = param1

   self.attr2 = param2

   self.attr3 = param3 #: Doc comment *inline* with attribute

   #: list of str: Doc comment *before* attribute, with type specified

   self.attr4 = [attr4]

   self.attr5 = None

   """str: Docstring *after* attribute, with type specified."""

   def public_method(self):

   """Performs operation blah."""

   """Summary line. #第一行简写函数描述

   # 空一行,对齐详细描述

   Extended description of function.

   # 空一行对齐,写args 的各个内容,变量名(类型):描述

   Args:

   arg1 (int): Description of arg1

   arg2 (str): Description of arg2

   # 空一行对齐,不同情况下的if else 返回值(类型):描述

   Returns:

   int/float/bool dtype: Description of return value

   """

  

  

Example

  最后完整的按照风格来写一个类的示例:

  

class Dog(object):

   """

   This is a class for Dog

   Dog class is the parents class of all dog, this class contain

   general attributes of dog and some common function of dogs, such as

   num legs, the voice fucntion, the runing functions.

   Attributes:

   name: A string of dogs name

   kind: A string of dogs family

   age: A integer of dog years

   gender: A boolean gender of dog, male=1 of famle=0

   legs A integer if dogs legs

   weight: A float of dogs weight

   size: A string of dogs, one of big, middle, smal

   """

   def __init__(self,args,gender,size):

   """initialize dog class, all attributes pass in with args, which is a dict or indepent params

   Input contain dict and str params, also there is private attribute

   Args:

   args.name(str): dog name

   args.kind(str): dog family

   args.age(int) : dog age

   gender(bool) : dog gender, male=1,famale=0

   args.weight(float): dog weight

   size(str) : dog size

   """

   self.name = args.name

   self.kind = args.kind

   self.age = args.age

   self.weight = args.weight

   # __legs(int) : dog legs,privite attribute, not the inputs params,写在前面用#做注释,不属于输入的参数的初始化

   self.__legs = 4

   """写在后面用三引号__legs(int) : dog legs,privite attribute"""

   self.size = size

   self.gender = gender

   def voice(self,size):

   """This is dog speak fucntion

   Different dog with different voice

   which related to the size,age and kind

   Args:

   size(str): dog size

   age(int) : dog age

   kind(srt): dog kind

   Returns:

   None, just print the voice

   """

   if size==big:

   print(Big WangWang)

   elif size ==middle:

   print(M wang)

   elif size==small:

   print(Miao)

   # 附注:return 可从任意深度跳出函数,None

  

  

  

Python类的简单写法

  

class MyClass:

   name =

   age = 0

   __weight = 0 #私有变量

   def __init__(self, n, a, w): #self必须作为函数的第一个参数

   self.name = n

   self.age = a

   self.__weight = w

   def speak(self):

   print(%s 说:我 %s 岁%(self.name, self.age))

  x = MyClass(yao, 10, 30)

  x.speak()

  以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • python语言基本语句用法总结大全,python语言基本语句用法总结怎么写
  • python语言基本语句用法总结大全,python语言基本语句用法总结怎么写,python语言基本语句用法总结
  • python是一种什么类型的计算机语言,python语言是一种什么类型的编程语言
  • Python的保留字符,python语言中的保留字
  • python的指数e怎么表示,指数函数在python语言中怎么表示
  • python语言合法的变量命名,在python中变量的命名要注意哪些问题
  • python变量命名可以用中文吗,下面哪一个不是python语言合法的变量命名
  • Python分词库,用python语言中文分词第三方库jieba对文件data
  • python中复数类型的实部和虚部都是浮点数,python语言中,复数类型中实数部分和虚数部分
  • 用python语言判断一个数是否为素数,Python判断是不是素数
  • python语句变量命名规则,python语言命名变量规则
  • 简述python语言程序的特点,以下关于python语言技术特点
  • matlab中for循环的用法例子,python语言for循环语句
  • Python主网站,python语言的官方网站
  • 用python开发的软件有哪些,哪些工具可以使用python语言开发软件
  • 留言与评论(共有 条评论)
       
    验证码: