Catalog
enumeration Enumerate
object introspection
dir
type and id
inspect modular
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')]
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 .
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 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 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 .