首页 > 编程语言 > 详细

python 函数的定义

时间:2018-07-16 00:26:29      阅读:220      评论:0      收藏:0      [点我收藏+]
格式:

        def 函数名(参数):

                内容 / pass;# pass为占位符,空

               return; / None;


一、普通函数

#!/usr/bin/python

def my_abs(x): #空函数
    None;


def my_pass(): #空函数
    pass;


def my_print(x):
    print "haha"


def my_try(a):
    return None;


二、函数参数检查:isinstance(参数,参考的tuple)

def your_age(x)

    if not isinstance(x, (int, float)):   #检查参数x 是否为tuple中的int/float类型

        raise TypeError('the age's type is error, please check again');  #出错信息打印,类似于C的perror();

    if x < 18:

        print 'teenager';

    if x >= 18:

        print 'adult';

    return None;


三、返回多个值

def my_abs(a, b)

    ret1 = abs(a);

    ret2 = abs(b);

    return ret1, ret2;  #其实返回值是tuple类型,ret1,re2是其元素;



python 函数的定义

原文:http://blog.51cto.com/13502993/2143906

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!