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

Python一些内置函数

时间:2016-05-17 11:47:15      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:python

1、callable函数介绍

  介绍任何对象作为参数,如果参数对象是可调用的,返回True;否则返回False。

In [1]: import string

In [3]: string.punctuation

Out[3]: ‘!"#$%&\‘()*+,-./:;<=>?@[\\]^_`{|}~‘

In [4]: string.join

Out[4]: <function string.join>

In [6]: callable(string.punctuation)

Out[6]: False


In [7]: callable(string.join)

Out[7]: True

2、getattr函数介绍

   使用getattr函数,可以得到一个直到运行时才直到名称的函数的引用。


In [8]: li = ["larry","curly"]


In [9]: li.pop

Out[9]: <function pop>


In [10]: getattr(li,"pop")  //相当于li.pop

Out[10]: <function pop>


In [11]: getattr(li,"append")("moe")  //相当于li.append("moe")


In [12]: li

Out[12]: [‘larry‘, ‘curly‘, ‘moe‘]


In [13]: getattr({},"clear") //确定{}字典里面有没有clear这个方法。

Out[13]: <function clear>


In [14]: getattr((),"pop")//因为()元组里面并没有POP这个方法所以报错。

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/root/<ipython-input-14-e1701c6ecc00> in <module>()

----> 1 getattr((),"pop")


AttributeError: ‘tuple‘ object has no attribute ‘pop‘


In [15]: 


Python一些内置函数

标签:python

原文地址:http://wushipeng.blog.51cto.com/6496437/1774215

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