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.

猜你喜欢

转载自blog.csdn.net/newdas123/article/details/80913542