Python的__builtins__模組中的一些要點知識

來源:互聯網
上載者:User

Python的__builtins__模組中的一些要點知識

   這篇文章主要介紹了Python的__builtins__模組中的一些要點知識,是Python學習中的基礎,需要的朋友可以參考下

  1.isinstance函數:除了以一個類型作為參數,還可以以一個類型元組作為參數。

  ?

1

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

  2.getattr函數:可以給一個預設值,以免觸發錯誤。

  ?

1

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

  3.type函數:即可以得到一個對象的類型,也可以直接由它建立一個新類型:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

>>> 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))

['__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)

>>>

  4.issubclass(bool,int)==True

  5.numbers.Number是所有數字類型的基類

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

  7.iter函數除了iter(object)形式,還有iter(callable,sentinel)也是返回一個iterator對象

  ?

1

2

3

4

5

6

7

8

>>> 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

>>>

  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

2

3

4

5

6

7

8

>>> 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

>>>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.