Python内置函数(四)—— f/g/h/i

一、float(x) —— 用于将整数和字符串转换成浮点数,返回浮点数

>>> float(2)
2.0
>>> float(-66)
-66.0
>>> float("111")
111.0

二、format() —— 格式化字符串  通过{ }和 :来替代以前的%

print("{} {}".format("hellod", "world"))          # 不指定位置
print("{0} {1}".format("hellod", "world"))        # 指定位置
print("{1} {0} {1}".format("hellod", "world"))    # 指定位置

输出结果:

hellod world
hellod world
world hellod world

三、frozenset(iterable) —— 返回一个冻结的集合,冻结后集合不能添加或删除任何元素

    returns: 返回新的frozenset对象,如果不提供任何参数,默认会生成空集合;

>>> a = frozenset(range(10))
>>> a
frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})

冻结有什么用。。  搞不懂

四、getattr(object, name[, default]) —— 返回对象的属性值

         object  : 对象

         name   :字符串,对象属性

         default :默认返回值,如果不提供该参数,在没有对应属性时,将触发AttributError。

>>> class Animal(object):        
...     fox = 1
...
>>> print(getattr(a, "fox"))          # 获取fox属性值
1
>>> print(getattr(a, "fox2"))        # fox2属性不存在,触发异常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Animal' object has no attribute 'fox2'
>>> print(getattr(a, "fox2", 3))     # fox2 属性不存在,但设置了默认值
3

五、globals() —— 以字典类型返回当前位置的所有全局变量,包括导入的变量

>>> a = 'Chaclin'
>>> print(globals()) {'__name__': '__main__', '__doc__': None, '__package__': None,
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>,
'a': 'Chaclin'}

六、hasattr(object, name) —— 判断对象是否包含对应的属性;

    object: 对象

    name : 字符串,属性名 

returns :如果对象有该属性返回True, 否则返回False

class Animal(object):
    fox = 1
    cat = 2
    dog = 3
a = Animal()
print(hasattr(a, "fox"))
print(hasattr(a, "cat"))
print(hasattr(a, "dog"))
print(hasattr(a, "snake"))

执行结果:
True
True
True
False

七、hash(object) —— 获取object(字符串/数值等)的的哈希值;返回对象的哈希值

>>> hash("test")
-1309918241118859936
>>> hash('test')
-1309918241118859936
>>> hash(1)
1
>>> hash(str([1, 2, 3]))    # 集合需要转换成字符串??
7463945498859899307

八、help([object]) —— 用于查看函数或模块用途的详细说明, 返回对象帮助信息

九、hex(x) —— 将一个指定数字转换为16进制数,返回一个字符串,以0x开头;

>>> hex(-255)
'-0xff'
>>> hex(255)
'0xff'
>>> type(hex(22))
<class 'str'>

十、id([object]) —— 返回对象的内存地址

>>> a = 1
>>> id(a)
140705502192464

十一、input([prompt]) —— 接收一个标准输出数据,返回string类型

>>> a = input("input:")
input:123
>>> a
'123'
>>> type(a)
<class 'str'>

十二、int(x, base=10) —— 将一个字符串或数字转换为整型,返回整型数据;

    x:  字符串或数字

    base:进制数,默认十进制

return:返回整型数据

print(int())
print(int(3))
print(int(3.6))
print(int("12", 16))     # base参数,x要以字符串形式传入
print(int("0xa", 16))   # base参数,x要以字符串形式传入
print(int("10", 8))      # base参数,x要以字符串形式传入

执行结果:
0
3
3
18
10
8

 

十三、isinstance(object, classinfo) —— 判断一个对象是否是一个已知的类型

        object: 实例对象

        classinfo: 可以是直接或间接类名、基本类型或者由他们组成的元组

return: 如果object是classinfo的类型,相同则返回True, 否则返回False

>>> a = 2
>>> isinstance(a, int)
True
>>> isinstance(a, str)
False
>>> isinstance(a, (str, list,int))  # 是元组中的一个返回True
True

十四、issubclass(class, classinfo) —— 判断参数是否是类型classinfo的子类

>>> class A():
...     pass
...
>>> class B(A):
...     pass
...
>>> issubclass(B, A)
True

十五、iter(object[, sentinel]) —— 生成迭代器,返回迭代器对象

    object: 支持迭代的集合对象

    sentinel: 如果传入此参数,则object必须是一个可调用的对象(如,函数),此时,iter创建了一个迭代器对象,每次调用这个迭代器对象的__next__()方法时,都会调用object。(啥意思

>>> temp = [1, 2, 3]
>>> for i in iter(temp):
...     print(i)
...
1
2
3

 

posted @ 2019-05-21 16:40  Chaclin  阅读(132)  评论(0)    收藏  举报