• day②:一切皆对象+内置方法+int


    一.python基础
    对于python,一切事物都是对象,对象基于类创建。

    例子:
    li=[11,22,33]
    li.append(44)
    对象所具备的方法,例如:li列表的增删改查,都不是对象自己所有的,它只是去到这个类里面的方法执行的!


    二.数据内型的内置方法
    type() 查看对象的类型
    dir(类型名) 查看类中提供的所有功能
    help(类型名) 查看类中所有详细的功能
    help(类型名.功能名) 查看类中某功能的详细

    关于dir(),之后,你会看到两边带下划线和不带下划线的方法:
    __方法__ 内置方法,可能有多种执行方法,至少一种
    方法 非内置方法,只有一种执行方法,对象.方法

    int例子:
    (1)dir 查看所有属性
    1. >>> dir(int)
    2. ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
    3. >>> n1 = 1
    4. >>> n2 = 2
    5. >>> n1 + n2
    6. 3
    7. >>> n1.__add__(n2)
    8. 3
    9. #经过python的解析器解析,+号其实最后会转换为内置方法__add__

    (2)__float__
    1. def __float__(self):
    2. """ 转换为浮点类型 """
    3. """ x.__float__() <==> float(x) """
    4. pass
    5. >>> age=18
    6. >>> type(age) #整形
    7. <type 'int'>
    8. >>> age.__float__()
    9. 18.0
    10. >>> nage=age.__float__()
    11. >>> type(nage)
    12. <type 'float'> #转换为浮点型

    (3)__floordiv__
    1. ##地板除
    2. def __floordiv__(self, y):
    3. """ x.__floordiv__(y) <==> x//y """
    4. pass
    5. >>> 5/2
    6. 2
    7. >>> 5//2
    8. 2
    9. >>> 5.0/2
    10. 2.5
    11. >>> 5.0//2 #不保留小数后面的
    12. 2.0

    (4)__pos__
    1. def __pos__(self):
    2. """ x.__pos__() <==> +x """
    3. pass
    4. >>> 2*8
    5. 16
    6. >>> 2**8
    7. 256
    8. >>> n = 2
    9. >>> n.__pow__(8)
    10. 256

    (5)__index__
    1. def __index__(self):
    2. """ 用于切片,数字无意义 """
    3. """ x[y:z] <==> x[y.__index__():z.__index__()] """
    4. pass
    5. #为什么一个对象能切片啊,因为里面有index方法!

    (6)__cmp__
    1. help(int)
    2. ....
    3. | __cmp__(...)
    4. | x.__cmp__(y) <==> cmp(x,y)
    5. |
    6. ....
    7. >>> int("11",base=2) #base表示要转换为2进制
    8. 3
    9. >>> int("F",base=16) #base表示要转换为16进制
    10. 15
    11. >>> age=19
    12. >>> age.__cmp__(3) #19比3大,返回1
    13. 1
    14. >>> age.__cmp__(32) #19比32小,返回-1
    15. -1
    16. >>> age.__cmp__(19) #两个19比较,返回0
    17. 0
    18. >>> cmp(19,3) #19比3大,返回1
    19. 1
    20. >>> cmp(19,32) #19比32小,返回-1
    21. -1
    22. >>> cmp(19,19) #两个19比较,返回0
    23. 0

    (7)__divmod__
    1. ##需求:
    2. 99条数据
    3. 每个页面10条数据
    4. ?多少个页面
    5. def __divmod__(self, y):
    6. """ 相除,得到商和余数组成的元组 """
    7. """ x.__divmod__(y) <==> divmod(x, y) """
    8. pass
    9. >>> a = 99
    10. >>> a.__divmod__(10)
    11. (9, 9) (商,余数)
    12. >>> a=98
    13. >>> a.__divmod__(10)
    14. (9, 8) (商,余数)
    15. #余数为0,表示刚好分页
    16. #余数为1,表示商还要+1,才够分页

    (8)__radd__
    1. def __radd__(self, y):
    2. """ x.__radd__(y) <==> y+x """
    3. pass
    4. >>> n
    5. 2
    6. >>> n.__add__(3) #2+3
    7. 5
    8. >>> n+3
    9. 5
    10. >>> n+5
    11. 7
    12. >>> n.__radd__(3) #3+2
    13. 5
    14. >>> n.__radd__(5) #3+5
    15. 7

    (9)__hash__
    1. def __hash__(self):
    2. """如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等。"""
    3. """ x.__hash__() <==> hash(x) """
    4. pass
    5. >>> a="dfdsaufsuhadhfsaduhfsadfsdfdfsdafhsduifhsuidhfusdhfhsdufhsdfuhsdifisdjfiosdjfsdjfdsfdjds"
    6. >>> a.__hash__()
    7. 5423373101089537918

    (10)__hex__
    1. def __hex__(self):
    2. """ 返回当前数的 十六进制 表示 """
    3. """ x.__hex__() <==> hex(x) """
    4. pass
    5. >>> age
    6. 18
    7. >>> age.__hex__()
    8. '0x12'

    (11)__oct__
    1. def __oct__(self):
    2. """ 返回改值的 八进制 表示 """
    3. """ x.__oct__() <==> oct(x) """
    4. pass
    5. >>> age
    6. 18
    7. >>> age.__oct__()
    8. '022'

    (12)__neg__
    1. def __neg__(self):
    2. """ x.__neg__() <==> -x """
    3. pass
    4. >>> age
    5. 18
    6. >>> age.__neg__()
    7. -18




    end




  • 相关阅读:
    踏实每一个脚印——2019年12月复盘
    修改博客园markdown编辑器代码高亮风格的方法
    Hyperion: Building the Largest In memory Search Tree
    C++11——智能指针
    拷贝控制
    分布式系统常见概念
    extern和static使用
    APUE—UNIX文件系统
    C++的一些细节
    fork和僵尸进程
  • 原文地址:https://www.cnblogs.com/binhy0428/p/5125938.html
Copyright © 2020-2023  润新知