程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Advanced Series (9)

編輯:Python

Catalog

enumeration Enumerate

object introspection

dir

type and id

inspect modular


enumeration Enumerate

enumeration (enumerate) yes Python Built in functions . Its usefulness is difficult to explain in a simple line , But most newcomers , Even some advanced programmers don't realize it .

It allows us to traverse the data and automatically count , Here is an example :

for counter, value in enumerate(some_list):
    print(counter, value)

More than that ,enumerate Some optional parameters are also accepted , This makes it more useful .

my_list = ['apple', 'banana', 'grapes', 'pear']
for c, value in enumerate(my_list, 1):
    print(c, value)

Output :

(1, 'apple')

(2, 'banana')

(3, 'grapes')

(4, 'pear')

The above optional parameter allows us to customize which number to start enumerating .

You can also use it to create a list of tuples containing indexes , for example :

my_list = ['apple', 'banana', 'grapes', 'pear']
counter_list = list(enumerate(my_list, 1))
print(counter_list)

  Output : [(1, 'apple'), (2, 'banana'), (3, 'grapes'), (4, 'pear')]

object introspection

introspection (introspection), In the field of computer programming , The ability to determine the type of an object at run time .

It is Python One of the strengths of .Python Everything in is an object , And we can look at those objects carefully .Python It also includes many built-in functions and modules to help us .

dir

In this section, we will learn dir And how it facilitates us in introspection .

It is one of the most important functions for introspection . It returns a list , Lists the properties and methods owned by an object . Here is an example :

my_list = [1, 2, 3]
dir(my_list)

 Output: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

The introspection above gives us the names of all the methods of a list object . When you can't remember the name of a method , This will be very helpful . If we run dir() Without passing in parameters , Then it will return all the names of the current scope .

type and id

type Function returns the type of an object . for instance :

print(type(''))

 Output: <type 'str'>

print(type([]))

 Output: <type 'list'>

print(type({}))

Output: <type 'dict'>

print(type(dict))

 Output: <type 'type'>

print(type(3))

 Output: <type 'int'>

id() Function returns the unique name of any different kind of object ID, for instance :

name = "Yasoob"
print(id(name))

# Output: 139972439030304

inspect modular

inspect The module also provides many useful functions , To get information about active objects . For example , You can view the members of an object , Just run :

import inspect
print(inspect.getmembers(str))

 Output: [('__add__', <slot wrapper '__add__' of ... ...

There are many other ways to help self-examination . If you will , You can explore them .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved