dict()
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
返回初始化的字典,从一个位置参数和一个可能是空的关键字参数。
1.如果没有位置参数,产生空字典。
2.如果位置参数有,而且是映射对象,根据键值对产生一个字典。
>>> a = dict(one=1, two=2, three=3)#关键字参数
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1,2,3]))
>>> d = dict([('two',2),('three',3),('one',1)])
>>> e = dict({'two':2, 'three':3, 'one':1})
>>> a == b == c == d == e
True
>>>
dir()
中文说明:不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__()
,该方法将被调用。如果参数不包含__dir__()
,该方法将最大限度地收集参数信息。
参数object: 对象、变量、类型。
版本:该函数在python各个版本中都有,但是每个版本中显示的属性细节有所不同。使用时注意区别。
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'struct']
>>> dir(struct)
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack', 'unpack_from']
#定义Shape实例的dir方法
>>> class Shape(object):
... def __dir__(self):
... return ['area', 'perimeter', 'location']
...
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
>>> dir(Shape)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> dir()
['Shape', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 's', 'struct']
divmod(a,b)
中文说明:
divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数
返回结果类型为tuple
参数:
a,b可以为数字(包括复数)
版本:
在python2.3版本之前不允许处理复数,这个大家要注意一下
>>> divmod(9,2)
(4, 1)
>>> divmod(11,3)
(3, 2)
>>> divmod(1+2j,1+0.5j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't take floor or mod of complex number.