`
huangyiiiiii
  • 浏览: 118731 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

python is obvious !

阅读更多
初识 python 的时候常常会被一些陌生的概念绊倒,而当熟悉了这些概念之后你会发现它们原来是如此的简单明了!由于它们是如此的简单,所以我试图在*一*篇文章中就想把它们全部都介绍一遍。
  1. 万物皆对象,甚至一个小小的整数也不例外;而变量只是一个名字,它可以绑定到任何一个对象;使用内置函数 id 可以查看绑定的对象的 id ,语言的实现会保证两个不同对象的 id 是绝对不一样的。
    >>> a = 1
    >>> id(a)
    11541872
    >>> a = 2
    >>> id(a)
    11541860
    >>> b=1
    >>> id(b)
    11541872
  2. 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
    >>>
  3. 参数传递机制
    >>> 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}
  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
  5. new style class
    继承自 object 的都是 new style class,详细内容参考这里
  6. __new__
    参考
  7. staticmethod, classmethod
    参考
  8. 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
分享到:
评论

相关推荐

    Introduction to Python

    the other is to make it so complicated that there are no obvious deficiencies.” #### 三、Python学习准备 为了开始学习Python,首先需要安装Python软件。Python支持多种操作系统平台,包括OS X、Windows、...

    单元测试框架.txt

    The Zen of Python Python之禅 by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than ...

    ImagesBy360.zip

    The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. ...

    python学生作业管理系统论文.docx

    The test results show that the system can achieve the required functions, the running condition is acceptable, and there are no obvious drawbacks. 关键词:学生作业管理系统;Django;Mysql数据库 II. ...

    numpy-1.16.4+mkl-cp36-cp36m-win_amd64.whl

    NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Note that this guide is not a C++ tutorial: we assume that the reader is familiar with the language. Header Files In general, every .cc file should have an associated .h file. There are some common ...

    pythonisms

    2. **鸭子类型**:"If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck." 在Python中,如果对象的行为像某种类型,那么就把它当作那种类型处理,而不必关心它的实际...

    Docker-in-Action.pdf

    more obvious to a person with deeper Python ecosystem knowledge. After pouring over several installation guides, reading through configuration files, and fighting an epic battle through the deepest ...

    parsetoml:用于解析TOML文件的Nim库

    Nim是一种高级、静态类型、编译型的编程语言,设计目标是提供C++级别的效率,Python般的简洁性和Rust的安全性。它拥有丰富的标准库,支持多种编程范式,包括命令式、函数式、面向对象和泛型编程。在Nim中,开发库...

    最新版的DebuggingWithGDB7.05-2010

    4.12.1 A Non-obvious Benefit of Using Checkpoints . . . . . . . . . . . . . 25 25 28 29 30 30 31 32 32 35 38 40 41 ii 5 Debugging with gdb Stopping and Continuing . . . . . . . . . . . . . . . . . . ....

Global site tag (gtag.js) - Google Analytics