python Built-in Functions

bin(x:int)

In [9]: bin(-100)
Out[9]: '-0b1100100'

In [10]: f"{100:b}"
Out[10]: '1100100'

In [11]: f"{100:b}"
Out[11]: '1100100'

In [12]: f"{-100:b}"
Out[12]: '-1100100'

In [13]: f"{-100:#b}"
Out[13]: '-0b1100100'

chr ord

In [17]: ord("d")
Out[17]: 100

In [18]: chr(100)
Out[18]: 'd'

divmod(x:int, y:int)

In [20]: divmod(120, 11)
Out[20]: (10, 10)

eval


In [23]: x=1;eval("x+1")
Out[23]: 2

filter(function,iterable)

In [26]: list(filter(lambda x: x%2==0, range(10)))
Out[26]: [0, 2, 4, 6, 8]

isinstance(object, classinfo) classinfo可能是抽象基类

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

In [27]: isinstance(100, (str, ))
Out[27]: False

In [28]: isinstance(100, (str, int))
Out[28]: True

issubclass(class, classinfo) classinfo可能是抽象基类

Return true if class is a subclass (direct, indirect or virtual) of classinfo. A class is considered a subclass of itself. classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError exception is raised.

In [32]: import collections
In [33]: issubclass(int, collections.abc.Hashable)
Out[33]: True

map(function, iterable)

In [38]: list(map(lambda x:x*2, range(10)))
Out[38]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

property

class C:
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

vars([object])

Return the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute.

Objects such as modules and instances have an updateable __dict__ attribute; however, other objects may have write restrictions on their __dict__ attributes (for example, classes use a types.MappingProxyType to prevent direct dictionary updates).

Without an argument, vars() acts like locals(). Note, the locals dictionary is only useful for reads since updates to the locals dictionary are ignored.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值