Python __builtins__ module Pick Spike

Source: Internet
Author: User
Tags vars pprint

1.isinstance functions: In addition to a type as a parameter, you can also use a type tuple as a parameter.

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

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

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

3.type function: That is, you can get the type of an object, or you can create a new type directly from it:

1>>> Point=type (' Point', (object,), {'x'70A'y': 0})2>>> p=Point ()3>>>p.x,p.y4 (0, 0)5>>> P=point (3,8)6 7 Traceback (most recent):8File"<pyshell#55>", Line 1,inch<module>9P=point (3,8)Ten Typeerror:object () takes no parameters One>>>Pprint.pprint (dir (point)) A['__class__', -  '__delattr__', -  '__dict__', the  '__doc__', -  '__format__', -  '__getattribute__', -  '__hash__', +  '__init__', -  '__module__', +  '__new__', A  '__reduce__', at  '__reduce_ex__', -  '__repr__', -  '__setattr__', -  '__sizeof__', -  '__str__', -  '__subclasshook__', in  '__weakref__', -  'x', to  'y'] +>>> p.name='Source Point' ->>>P.name the 'Source Point' *>>>Pprint.pprint (dir (p)) $['__class__',Panax Notoginseng  '__delattr__', -  '__dict__', the  '__doc__', +  '__format__', A  '__getattribute__', the  '__hash__', +  '__init__', -  '__module__', $  '__new__', $  '__reduce__', -  '__reduce_ex__', -  '__repr__', the  '__setattr__', -  '__sizeof__',Wuyi  '__str__', the  '__subclasshook__', -  '__weakref__', Wu  'name', -  'x', About  'y'] $>>>deftostr (self): -     return '(%s,%s)'%(SELF.X,SELF.Y) -  ->>> Point.__str__=Tostr A>>>PrintP + (0,0) the>>>defInit (self,x,y): -self.x,self.y=x, y $  the      the>>> Point.__init__=Init the>>> P2=point (6,8) the>>>PrintP2 -(6,8) in>>>

4.issubclass (Bool,int) ==true

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

6.type (None) ==nonetype,none is a constant

The 7.iter function, in addition to the ITER (object) Form, also has ITER (Callable,sentinel) returning a iterator object

1>>>defGetrand ():2     ImportRandom3     returnRandom.randint (1,100)4 5>>> forIinchITER (getrand,50):PrintI#get all 1-100 random numbers before getting 50 for the first time6 732 19 82 28 30 41 100 39 71 29 45 30 94 77 62 26 25 19 82 20 55 20 43 738>>> forIinchITER (getrand,50):PrintI#get all 1-100 random numbers before getting 50 for the first time9 Ten22 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 One>>>

8.BaseException is the base class for all exceptions, exception is just the base class for all exceptions that don't exit.

9.locals/globals/vars/dir:

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

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

[3] 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 all properties of the module, and if obj is a Class object, dir (obj) returns all the properties of the class, followed by the property inherited from the base class, and if obj is an instance object, dir (obj) returns the property of the instance object, the property of the class to which it belongs, The property to which its owning class-base class inherits. "Any modification to a class object is bound to be reflected on its instance object, and any modifications 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 and not from the default zero.

>>> forI,nameinchEnumerate (['C','C + +','CSharp','Java','Python'],1):    Print '%d.%s'%(I,name)1. C2.c++3. CSharp4. Java5. Python>>>

Python __builtins__ module Pick Spike

Related Article

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.