Python __builtins__ module gleaning

大家讲道理
Release: 2016-11-07 17:27:40
Original
1163 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 "", line 1, in  p=Point(3,8) TypeError: object() takes no parameters >>> pprint.pprint(dir(Point)) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x', 'y'] >>> p.name='source point' >>> p.name 'source point' >>> pprint.pprint(dir(p)) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'x', 'y'] >>> def tostr(self): return '(%s,%s)'%(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(['C','C++','CSharp','Java','Python'],1): print '%d.%s'%(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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!