当前位置:Gxlcms > Python > Python的__builtins__模块中的一些要点知识

Python的__builtins__模块中的一些要点知识

时间:2021-07-01 10:21:17 帮助过:66人阅读

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

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

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

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

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

4.issubclass(bool,int)==True

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

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

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

  1. >>> def getrand():
  2. import random
  3. return random.randint(1,100)
  4. >>> for i in iter(getrand,50):print i,#获取第一次得到50之前的所有1-100的随机数
  5. 32 19 82 28 30 41 100 39 71 29 45 30 94 77 62 26 25 19 82 20 55 20 43 73
  6. >>> for i in iter(getrand,50):print i,#获取第一次得到50之前的所有1-100的随机数
  7. 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. >>>

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开始,而不是从默认的零开始。

  1. >>> for i,name in enumerate(['C','C++','CSharp','Java','Python'],1):
  2. print '%d.%s'%(i,name)
  3. 1.C
  4. 2.C++
  5. 3.CSharp
  6. 4.Java
  7. 5.Python
  8. >>>

人气教程排行