初识 python 的时候常常会被一些陌生的概念绊倒,而当熟悉了这些概念之后你会发现它们原来是如此的简单明了!由于它们是如此的简单,所以我试图在
*一*篇文章中就想把它们全部都介绍一遍。
暂时只想到这些,当然遗漏在所难免了,如有任何意见,欢迎评论 :)
update [2006-9-21]:
结合 callable 和 docorate ,其实 docorate 那个 log 的例子还可以这么写,似乎更好读一些:
- 万物皆对象,甚至一个小小的整数也不例外;而变量只是一个名字,它可以绑定到任何一个对象;使用内置函数 id 可以查看绑定的对象的 id ,语言的实现会保证两个不同对象的 id 是绝对不一样的。
>>> a = 1
>>> id(a)
11541872
>>> a = 2
>>> id(a)
11541860
>>> b=1
>>> id(b)
11541872 - callable 对象
函数、方法、类、实现了 __call__ 方法的实例对象 都是 callable 对象。callable 的意思就是在后面写个括号直接就可以进行调用了。调用内置函数 callable 可以检验一个对象是否 callable 对象。>>> def check(obj):
... if callable(obj):
... obj(1,2)
... else:
... print 'not a callable'
...
>>> def func(a,b):print a,b
...
>>> class Temp(object):
... def __init__(self,a,b):print a,b
... def __call__(self,a,b):print a,b
... def method(self,a,b):print a,b
...
>>> check(func)
1 2
>>> check(Temp)
1 2
>>> t = Temp(1,2)
1 2
>>> check(t)
1 2
>>> check(t.method)
1 2
>>> - 参数传递机制
>>> def a_func(a,b,c=1,d=2):print a,b,c,d
...
>>> a_func(1,2,d=4,c=3)
1 2 3 4
>>> a_func(1,2,3,d=4)
1 2 3 4
>>> a_func(1,2)
1 2 1 2
>>> args = (1,2)
>>> kw = dict(c=3,d=4)
>>> a_func(*args, **kw)
1 2 3 4
>>> def a_func(*args, **kw):
... print args
... print kw
...
>>> a_func(1,2,d=4,c=3)
(1, 2)
{'c': 3, 'd': 4} - docorate
一个装饰就是一个接受一个函数作为参数的函数,它返回的还是一个函数。
好像有点绕口,还是让代码说话吧:>>> def simple_log(func):
... def new_func(*arg, **kw):
... print 'enter',func.func_name
... func(*arg, **kw)
... print 'exit',func.func_name
... return new_func
...
>>> def log(some_args):
... def simple_log(func):
... def new_func(*arg, **kw):
... print some_args,'enter',func.func_name
... func(*arg, **kw)
... print some_args,'exit',func.func_name
... return new_func
... return simple_log
...
>>> def a_func(a,b):print a,b
...
>>> simple_log(a_func)(1,2)
enter a_func
1 2
exit a_func
>>> @simple_log
... def a_func(a,b):print a,b
...
>>> a_func(1,2)
enter a_func
1 2
exit a_func
>>> log('haha')(a_func)(1,2)
haha enter a_func
1 2
haha exit a_func
>>> @log('haha')
... def a_func(a,b):print a,b
...
>>> a_func(1,2)
haha enter a_func
1 2
haha exit a_func - new style class
继承自 object 的都是 new style class,详细内容参考这里 - __new__
参考 - staticmethod, classmethod
参考 - metaclass
参考
实例对象由 class 构造而成,而 class 便是由 metaclass 构造而成。
简单地说一个 metaclass 就是一个接受三个参数(class的名字,基类tuple,class 的属性字典)的 callable 对象,它返回一个 class 。在构建 class 的时候便会调用这个 callable 对象,并使用它返回的 class 。
所有内建类型的 metaclass 和 new style class 默认的 metaclass 都是 type>>> def meta(name, bases, classdict):
... print name
... print bases
... print classdict
... return type(name, bases, classdict)
...
>>> class Temp(object):
... __metaclass__ = meta
... a = 1
... def b():pass
...
Temp
(<type object="">,)
{'a': 1, '__module__': '__main__', 'b': <function at="" b="">, '__metaclass
__': <function at="" meta="">}
>>> class ATemp(Temp):
... __metaclass__ = meta
...
ATemp
(<class __main__.temp="">,)
{'__module__': '__main__', '__metaclass__': <function at="" meta="">}</function></class></function></function></type>
暂时只想到这些,当然遗漏在所难免了,如有任何意见,欢迎评论 :)
update [2006-9-21]:
结合 callable 和 docorate ,其实 docorate 那个 log 的例子还可以这么写,似乎更好读一些:
>>> class log(object):
... def __init__(self, someargs):
... self.args = someargs
... def __call__(self,func):
... def new_func(*args,**kw):
... print self.args,'enter',func.func_name
... func(*args,**kw)
... print self.args,'exit',func.func_name
... return new_func
...
>>> @log('haha')
... def a_func(a,b):print a,b
...
>>> a_func(1,2)
haha enter a_func
1 2
haha exit a_func