码迷,mamicode.com
首页 > 编程语言 > 详细

python 函数的定义

时间:2018-07-16 00:28:51      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:typeerror   err   tee   ret   check   def   信息   返回值   python   

格式:

        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 函数的定义

标签:typeerror   err   tee   ret   check   def   信息   返回值   python   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!