Python内建函数(D)

2019-03-06 12:53|来源: 网络

  •  dir([object])

说明:不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。

参数object: 对象、变量、类型。

示例:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import struct
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'struct']
>>> dir(struct)
['Struct', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from']
>>> class Person(object):
...     def __dir__(self):
...             return ["name", "age", "country"]
...
>>> dir(Person)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__','__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> tom = Person()
>>> dir(tom)
['age', 'country', 'name']

 

  • delattr(object, name)

说明:删除object对象名为name的属性。

参数object:对象。

参数name:属性名称字符串。

示例:

>>> class Person:
...     def __init__(self, name, age):
...             self.name = name
...             self.age = age
...
>>> tom = Person("Tom", 35)
>>> dir(tom)
['__doc__', '__init__', '__module__', 'age', 'name']
>>> delattr(tom, "age")
>>> dir(tom)
['__doc__', '__init__', '__module__', 'name']

 

  • dict([arg])

说明:创建数据字典。

示例:

>>> a = dict() #空字典
>>> a
{}
>>> b = dict(one = 1, two = 2)
>>> b
{'two': 2, 'one': 1}
>>> c = dict({'one':1, 'two':2})
>>> c
{'two': 2, 'one': 1}
>>> d= dict([['two', 2], ['one', 1]])
>>> d
{'two': 2, 'one': 1}
>>> e ={'two': 2, 'one': 1}
>>> e
{'two': 2, 'one': 1}

 

  • divmod(a, b)

说明:返回一个数据对,等价于( a // b, a % b)

参数abintlongfloat

示例:

>>> divmod(5,3)
(1, 2)
>>> divmod(5.5, 2.2)
(2.0, 1.0999999999999996)
>>> divmod(5.5, 2)
(2.0, 1.5)

相关问答

更多
  • 【区别】: 标准库函数都需要import xxx才能取得。 内建函数都在__builtins__里面,在global里直接就能用。 【补充】: 1.python中,我们可以通过对内建的比较函数进行自定义,来实现运算符重载。 我们常用的比较运算符有 大于 > 对应的内建比较函数为 __gt__() 大于等于 >= 对应的内建比较函数为 __ge__() 等于 == 对应的内建比较函数为 __eq__() 小于 < 对应的内建比较函数为 __lt__() 小于等于 <= 对应的内建比较函数为 __le__() ...
  • writer = open(‘/tmp.log', 'a', encoding='utf-8') # 以追加模式打开tmp.log文件,编码为utf-8 writer.write('开始处理...') # 写入内容 writer.write('%(tile)s %(author)s %(size)d %(summary)s' % book) writer.write('处理完毕') writer.flush() # 刷新缓存 writer.close() # 关闭文件
  • /usr/lib/python2.7 2.7也可能是3.* 文件夹里面 很多 .Py 的文件 和 .pyc 的文件 前者是源文件,后者已经进行了预编译 这是模块,模块中设计的函数就在里面。 比如 re、socket、string。 内建函数可能也在某个文件中,没仔细找
  • 不认识的函数可以在shell 下运行help(你的函数) 建议找本教程看,偏手册的有python in a nutshell
  • >>> import __builtin__ >>> dir(__builtin__) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'Buffer Error', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'Environme ntError', 'Exception', 'False', 'FloatingPointErro ...
  • 字典(dict)下列字典的平均情况基于以下假设: 1. 对象的散列函数足够撸棒(robust),不会发生冲突。 2. 字典的键是从所有可能的键的集合中随机选择的。小窍门:只使用字符串作为字典的键。这么做虽然不会影响算法的时间复杂度,但会对常数项产生显著的影响,这决定了你的一段程序能多快跑完。操作平均情况最坏情况复制[注2]O(n)O(n)取元素O(1)O(n)更改元素[注1]O(1)O(n)删除元素O(1)O(n)遍历[注2]O(n)O(n) 注: [1] = These operations rely o ...
  • 三种⽅法可以查看内建模块的代码,以random模块为例: 1. 使⽤help(random) 2. 利⽤random.__file__查看位置,然后打开后缀名为.py的⽂件。 3. 命令⾏输⼊python -v,然后倒⼊模块时,会⾃动显⽰位置。
  • 不是这样的。 方法的概念,大概等同于函数,内建函数在python中指的是,无需import即可使用的函数。 它们位于一个叫做__builtin__的模块里。
  • dir(...) dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it: No argument: the names in the current scope. Module object: the module attributes. ...
  • 这是一个有趣的问题,因为如果键是另一个非索引类型(比如整数),那么就不会产生错误,这是由于一系列细微的事情造成的: 排序(mydict,...)尝试迭代一个字典使用相当于iter(mydict) ,它将调用mydict.__iter__() 迭代字典会产生键 ,实际上iter(mydict)和mydict.iterkeys()是一样的。 您的密钥是字符串,并且由于字符串是可索引的,因此itemgetter(1)将对字符串值起作用,从而获取字符串中的第二个字符。 如果任何字符串的长度为1个字符,那么您的代码将 ...