Python _ builtins _ module collection, python _ builtins _

Source: Internet
Author: User
Tags pprint

Python _ builtins _ module collection, python _ builtins _

1. isinstance function: in addition to a type as a parameter, you can also use a type tuples as a parameter.

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

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

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

3. type Function: You can obtain the type of an object, or you can directly create a new type:

 1 >>> Point=type('Point',(object,),{'x':0,'y':0}) 2 >>> p=Point() 3 >>> p.x,p.y 4 (0, 0) 5 >>> p=Point(3,8) 6  7 Traceback (most recent call last): 8   File "<pyshell#55>", line 1, in <module> 9     p=Point(3,8)10 TypeError: object() takes no parameters11 >>> pprint.pprint(dir(Point))12 ['__class__',13  '__delattr__',14  '__dict__',15  '__doc__',16  '__format__',17  '__getattribute__',18  '__hash__',19  '__init__',20  '__module__',21  '__new__',22  '__reduce__',23  '__reduce_ex__',24  '__repr__',25  '__setattr__',26  '__sizeof__',27  '__str__',28  '__subclasshook__',29  '__weakref__',30  'x',31  'y']32 >>> p.name='source point'33 >>> p.name34 'source point'35 >>> pprint.pprint(dir(p))36 ['__class__',37  '__delattr__',38  '__dict__',39  '__doc__',40  '__format__',41  '__getattribute__',42  '__hash__',43  '__init__',44  '__module__',45  '__new__',46  '__reduce__',47  '__reduce_ex__',48  '__repr__',49  '__setattr__',50  '__sizeof__',51  '__str__',52  '__subclasshook__',53  '__weakref__',54  'name',55  'x',56  'y']57 >>> def tostr(self):58     return '(%s,%s)'%(self.x,self.y)59 60 >>> Point.__str__=tostr61 >>> print p62 (0,0)63 >>> def init(self,x,y):64     self.x,self.y=x,y65 66     67 >>> Point.__init__=init68 >>> p2=Point(6,8)69 >>> print p270 (6,8)71 >>> 

4. issubclass (bool, int) = True

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

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

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

1 >>> def getrand (): 2 import random 3 return random. randint (1,100) 4 5 >>> for I in iter (getrand, 50): print I, # obtain the random number of all 1-100 values before 50 for the first time. 6 7 32 19 82 28 30 41 100 39 71 29 45 30 94 77 62 26 25 19 82 20 55 43 73 8>> for I in iter (getrand, 50): print I, # obtain the random number of all 1-100 values before 50 for the first time 9 10 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 8511 >>>

8. BaseException is the base class of all exceptions. Exception is only the base class of all exceptions without exit.

9. locals/globals/vars/dir:

[1] locals/globals is a simple local/Global Object dict relative to the current scope;

[2] vars () = locals (), vars (obj) = obj. _ dict __

[3] No parameter, set (dir () = set (locals (). keys (); if hasattr (obj, '_ dir _') => dir (obj) = obj. _ dir _ (); otherwise, if obj is a module object, dir (obj) returns all attributes of the module. If obj is a class object, dir (obj) all the attributes of the class are returned, followed by the attributes inherited from the base class. If obj is an instance object, dir (obj) the returned result is the proprietary attributes of the instance object, the attributes of its class, and the attributes inherited from its base class. [Any modification to the 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 property masking .]

10. enumerate function: enumerate (obj, [start]). If start is defined, the ordinal number starts from start instead of the default zero.

>>> for i,name in enumerate(['C','C++','CSharp','Java','Python'],1):    print '%d.%s'%(i,name)    1.C2.C++3.CSharp4.Java5.Python>>> 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.