zoukankan      html  css  js  c++  java
  • Python小白学习之路(十八)—【内置函数三】

    一、对象操作

    help()

    功能:返回目标对象的帮助信息

    举例:
    print(help(input))
    
    #执行结果
    Help on built-in function input in module builtins:
    
    input(prompt=None, /)
    Read a string from standard input. The trailing newline is stripped.
    
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.
    
    None

    dir()

    功能:返回对象或者当前作用域内的属性列表

    举例:
    import math
    print(dir(math))
    
    #执行结果
    ['__doc__', '__loader__', '__name__', '__package__', 
    '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
    'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 
    'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 
    'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma',
    'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 
    'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 
    'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 
    'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

    id()


    功能:返回目标对象的唯一标识符

    举例:
    a = 'xhg'
    b = 'dhg'
    print(id(a), id(b))
    
    #执行结果
    4512992 4513088

    hash()

    功能:获取目标对象的哈希值

    哈希值:

    又称:散列函数(或散列算法,又称哈希函数,英语:Hash Function)
    是一种从任何一种数据中创建小的数字“指纹”的方法。
    散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来。
    该函数将数据打乱混合,重新创建一个叫做散列值(hash values,hash codes,hash sums,或hashes)的指纹。
    散列值通常用一个短的随机字母和数字组成的字符串来代表。
    好的散列函数在输入域中很少出现散列冲突。
    在散列表和数据处理中,不抑制冲突来区别数据,会使得数据库记录更难找到。

    举例:
    a = 'xhg'
    b = 'dhg'
    print(hash(a), hash(b))
    
    #执行结果
    -1732189627 760535467

    type()

    功能:
    返回对象的类型 type(object) -> the object's type
    根据传入的参数创建一个新的类型 type(name, bases, dict) -> a new type

    举例:
    a = [1, 2, (1, 2), 'xhg']
    print(type(a))
    #执行结果
    <class 'list'>

    len()

    功能:返回对象的长度

    举例:
    print(len('xhg'), len(bytes('小伙郭','utf-8')), len([1, 2, 3]))
    #执行结果
    3 9 3

    ascii()

    功能:返回对象的可打印表字符串表现方式

    print(ascii('AA'), ascii(123) ,ascii('中国'))
    举例:
    #执行结果
    'AA' 123 'u4e2du56fd'
    #‘中国’非ASCII字符

    format()


    功能:格式化
    在学习记录九中有详细介绍

    vars()

    功能:返回当前作用域内的局部变量和其值组成的字典,或者返回对象的属性列表


    二、反射操作

    isinstance()


    功能:判断对象是否是类或者类型元组中任意类元素的实例

    print(isinstance('xhg', str))
    print(isinstance('xhg', int))
    
    #执行结果
    True
    False

    issubclass()


    功能:判断类是否是另外一个类或者类型元组中任意类元素的子类

    print(issubclass(int, str))
    print(issubclass(bool, int))
    
    #执行结果
    False
    True

    三、变量操作

    globals()

    功能:全局变量

    locals()

    功能:当前作用域变量

    四、交互操作

    print()

    功能:打印输出

    input()

    功能:读取用户输入值

  • 相关阅读:
    JAVA中重写equals()方法为什么要重写hashcode()方法说明
    深入浅出UML类图
    UML解惑:图说UML中的六大关系
    maven自动打包上传nexus仓库配置
    vi查找替换命令详解
    Maven运行JUnit测试(http://www.360doc.com/content/13/0927/15/7304817_317455642.shtml)
    Maven 排除依赖jar包
    2、【Spark】Spark环境搭建(集群方式)
    1、【Spark】Spark安装
    Centos7 执行firewall-cmd –permanent –add-service=mysql报错“ModuleNotFoundError: No module named 'gi'”
  • 原文地址:https://www.cnblogs.com/guoruxin/p/9961996.html
Copyright © 2011-2022 走看看