Heim  >  Artikel  >  Backend-Entwicklung  >  Python __builtins__模块拾穗

Python __builtins__模块拾穗

大家讲道理
大家讲道理Original
2016-11-07 17:27:401176Durchsuche

1.isinstance函数:除了以一个类型作为参数,还可以以一个类型元组作为参数。

isinstance(obj,basestring)===isinstance(obj,(str,unicode))

2.getattr函数:可以给一个默认值,以免触发错误。

writte=getattr(obj,'write',sys.stdout.write)

3.type函数:即可以得到一个对象的类型,也可以直接由它创建一个新类型:

>>> Point=type('Point',(object,),{'x':0,'y':0})
>>> p=Point()
>>> p.x,p.y
(0, 0)
>>> p=Point(3,8)
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in <module>
    p=Point(3,8)
TypeError: object() takes no parameters
>>> pprint.pprint(dir(Point))
[&#39;__class__&#39;,
 &#39;__delattr__&#39;,
 &#39;__dict__&#39;,
 &#39;__doc__&#39;,
 &#39;__format__&#39;,
 &#39;__getattribute__&#39;,
 &#39;__hash__&#39;,
 &#39;__init__&#39;,
 &#39;__module__&#39;,
 &#39;__new__&#39;,
 &#39;__reduce__&#39;,
 &#39;__reduce_ex__&#39;,
 &#39;__repr__&#39;,
 &#39;__setattr__&#39;,
 &#39;__sizeof__&#39;,
 &#39;__str__&#39;,
 &#39;__subclasshook__&#39;,
 &#39;__weakref__&#39;,
 &#39;x&#39;,
 &#39;y&#39;]
>>> p.name=&#39;source point&#39;
>>> p.name
&#39;source point&#39;
>>> pprint.pprint(dir(p))
[&#39;__class__&#39;,
 &#39;__delattr__&#39;,
 &#39;__dict__&#39;,
 &#39;__doc__&#39;,
 &#39;__format__&#39;,
 &#39;__getattribute__&#39;,
 &#39;__hash__&#39;,
 &#39;__init__&#39;,
 &#39;__module__&#39;,
 &#39;__new__&#39;,
 &#39;__reduce__&#39;,
 &#39;__reduce_ex__&#39;,
 &#39;__repr__&#39;,
 &#39;__setattr__&#39;,
 &#39;__sizeof__&#39;,
 &#39;__str__&#39;,
 &#39;__subclasshook__&#39;,
 &#39;__weakref__&#39;,
 &#39;name&#39;,
 &#39;x&#39;,
 &#39;y&#39;]
>>> def tostr(self):
    return &#39;(%s,%s)&#39;%(self.x,self.y)
>>> Point.__str__=tostr
>>> print p
(0,0)
>>> def init(self,x,y):
    self.x,self.y=x,y
     
>>> Point.__init__=init
>>> p2=Point(6,8)
>>> print p2
(6,8)
>>>

   

4.issubclass(bool,int)==True

5.numbers.Number是所有数字类型的基类

6.type(None)==NoneType,None是一个常量

7.iter函数除了iter(object)形式,还有iter(callable,sentinel)也是返回一个iterator对象

>>> def getrand():
    import random
    return random.randint(1,100)
>>> for i in iter(getrand,50):print i,#获取第一次得到50之前的所有1-100的随机数
32 19 82 28 30 41 100 39 71 29 45 30 94 77 62 26 25 19 82 20 55 20 43 73
>>> for i in iter(getrand,50):print i,#获取第一次得到50之前的所有1-100的随机数
22 54 14 25 60 65 16 80 61 5 48 61 2 30 90 98 70 10 55 45 23 72 87 39 70 3 84 85
>>>

   

8.BaseException是一切exceptions的基类,Exception只是一切不exit的exceptions的基类

9.locals/globals/vars/dir:

[1]locals/globals很简单,是相对于当前作用域的本地/全局对象dict;

[2]vars()==locals(),vars(obj)==obj.__dict__

[3]没有参数,set(dir())==set(locals().keys());if hasattr(obj,'__dir__')=>dir(obj)==obj.__dir__();否则,如果obj是模块对象,dir(obj)返回的是模块的所有属性;如果obj是类对象,dir(obj)返回的是类的所有属性,然后是从基类继承来的属性;如果obj是实例对象,dir(obj)返回的是实例对象专有的属性、其所属类的属性、其所属类基类继承来的属性。【对类对象的任何修改,必将反映到其实例对象上;对基类的任何修改,也必将反映到派生类上。当然,属性遮蔽的情况除外。】

10.enumerate函数:enumerate(obj,[start]),如果定义了start,则序数将从start开始,而不是从默认的零开始。

>>> for i,name in enumerate([&#39;C&#39;,&#39;C++&#39;,&#39;CSharp&#39;,&#39;Java&#39;,&#39;Python&#39;],1):
    print &#39;%d.%s&#39;%(i,name)
1.C
2.C++
3.CSharp
4.Java
5.Python
>>>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Python实例获取mp3文件的tag信息Nächster Artikel:ip、数字的互转