Python __builtins__ module gleaning

大家讲道理
Release: 2016-11-07 17:27:40
Original
1226 people have browsed it

1.isinstance function: In addition to taking a type as a parameter, it can also take a type tuple as a parameter.

isinstance(obj,basestring)===isinstance(obj,(str,unicode))
Copy after login

2.getattr function: You can give a default value to avoid triggering errors.

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

3.type function: You can get the type of an object or create a new type directly from it:

>>> 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)
>>>
Copy after login

4.issubclass(bool,int)==True

5.numbers. Number is the base class of all numerical types

6.type(None)==NoneType, None is a constant

7.In addition to the iter(object) form, the iter function also returns an iterator object (callable, sentinel)

>>> 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
>>>
Copy after login

8.BaseException is the base class of all exceptions, Exception is just the base class of all exceptions that do not exit

9.locals/globals/vars/dir:

[1]locals/globals is very simple, Is a local/global object dict relative to the current scope;

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

[3] has no parameters, set(dir()) ==set(locals().keys()); if hasattr(obj,'__dir__')=>dir(obj)==obj.__dir__(); Otherwise, if obj is a module object, dir(obj) returns are all attributes of the module; if obj is a class object, dir(obj) returns all the attributes of the class, and then the attributes inherited from the base class; if obj is an instance object, dir(obj) returns the instance object Some attributes, attributes of the class to which it belongs, and attributes inherited from the base class of the class to which it belongs. [Any modification to a class object will be reflected on its instance object; any modification to the base class will also be reflected on the derived class. Of course, except for the case of attribute masking. 】

10.enumerate function: enumerate(obj,[start]), if start is defined, the ordinal will start from start, not from the default zero.

>>> 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
>>>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template