鱼C论坛

 找回密码
 立即注册
查看: 1865|回复: 0

[技术交流] python学习笔记之函数和函数文档

[复制链接]
发表于 2017-7-7 08:55:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
函数
  1.     >>> def MyFirstFunction():
  2.         print('First Function')
  3.         print('Exciting')
  4.         print('...............')
复制代码
def定义函数,后面接函数名,冒号不可少
  1.     >>> MyFirstFunction()
  2.     First Function
  3.     Exciting
  4.     ...............
复制代码
带有参数的函数
  1.     >>> def MySecondFunction(name):
  2.         print(name+'you')
  3.     >>> MySecondFunction('Adventor')
  4.     Adventoryou
复制代码
带有返回值的函数
  1.     >>> def add(num1,num2):
  2.        return (num1 + num2)
  3.     >>> add(5,6)
  4.     11
复制代码
函数文档
  1. >>> def Function(name):
  2.    '函数文档'
  3.    #函数文档只是一个形式,占据一个参数位置
  4.    print('输入的字符是:'+name)


  5. >>> Function('函数文档')
  6. 输入的字符是:函数文档
  7. >>> Function.__doc__
  8. '函数文档'
复制代码
  • 如上,函数文档是以字符串的形式写在函数中的,查看函数文档以函数名和双下横线查看
  • 也可以使用help命令查看,help命令查看结果更加易读,如下
    1.   >>> help(Function)
    2.   Help on function Function in module __main__:

    3.   Function(name)
    4.       函数文档

    5.   >>> print.__doc__
    6.   "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\nPrints the values to a stream, or to sys.stdout by default.\nOptional keyword arguments:\nfile:  a file-like object (stream); defaults to the current sys.stdout.\nsep:   string inserted between values, default a space.\nend:   string appended after the last value, default a newline.\nflush: whether to forcibly flush the stream."
    7.   >>> help(print)
    8.   Help on built-in function print in module builtins:

    9.   print(...)
    10.    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    11.       Prints the values to a stream, or to sys.stdout by default.
    12.       Optional keyword arguments:
    13.       file:  a file-like object (stream); defaults to the current sys.stdout.
    14.       sep:   string inserted between values, default a space.
    15.       end:   string appended after the last value, default a newline.
    16.       flush: whether to forcibly flush the stream.
    复制代码


评分

参与人数 2鱼币 +4 收起 理由
小甲鱼 + 2
康小泡 + 2

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 00:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表