函数定义
函数返回多个值的时候,返回的是元组类型
方法和函数的区别
方法一定是和具体的对象相对应的
from types import FunctionType,MethodType
class Foo(object):
def init(self):
self.name="haiyan"
def func(self):
print(self.name)
obj = Foo()
print(isinstance(obj.func,FunctionType)) #False
print(isinstance(obj.func,MethodType)) #True #说明这是一个方法
print(isinstance(Foo.func,FunctionType)) #True #说明这是一个函数。
print(isinstance(Foo.func,MethodType)) #False
函数调用
说明:默认参数后不能再出现非默认参数。示例如下:
(1)f(a, b = 2) 这样定义合法!
(2)f(b = 2, a) 这样定义非法!因为在默认参数后又出现了非默认参数a。