python精要(66)—类(3)-文档字符串,注释文档

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 22 08:35:42 2021

@author: myhaspl
"""


class Point:
    def __new__(cls,x=0,y=0):
        'new调用此方法后,再调用__init__构造函数,可在此方法中对父类的__new__方法进行调用'
        print("new")
        return object.__new__(cls)
    def __init__(self,x=0,y=0):
        print("init")
        self.set(x,y)
    def set(self,x,y):
        "设置点对象的坐标值"
        self.x=x
        self.y=y
    def setFromPoint(self,point):
        """从另一个相同的类对象设置
        本类对象的坐标值。
        """
        self.x=point.x
        self.y=point.y



b=Point(10,20)
print(b.x)
print(b.y)
b.set(101, 102)
print(b.x)
print(b.y)

a=Point()
print(a.x)
print(a.y)
a.setFromPoint(b)
print(a.x)
print(a.y)

文档字符串用于注释文档
1、‘’或“”完成单行文档注释
2、""" “”"完成多行文档注释
3、使用help查看文档

help(Point)
Help on class Point in module __main__:

class Point(builtins.object)
 |  Point(x=0, y=0)
 |  
 |  Methods defined here:
 |  
 |  __init__(self, x=0, y=0)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  set(self, x, y)
 |      设置点对象的坐标值
 |  
 |  setFromPoint(self, point)
 |      从另一个相同的类对象设置
 |      本类对象的坐标值。
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(cls, x=0, y=0)
 |      new调用此方法后,再调用__init__构造函数,可在此方法中对父类的__new__方法进行调用
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页