首页 > 其他 > 详细

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

时间:2016-01-13 00:31:00      阅读:161      评论:0      收藏:0      [点我收藏+]

一.python基础

对于python,一切事物都是对象,对象基于类创建。
 
例子:
li=[11,22,33]
li.append(44)
对象所具备的方法,例如:li列表的增删改查,都不是对象自己所有的,它只是去到这个类里面的方法执行的!
 
 
二.数据内型的内置方法
type() 查看对象的类型
dir(类型名) 查看类中提供的所有功能
help(类型名) 查看类中所有详细的功能
help(类型名.功能名) 查看类中某功能的详细
 
关于dir(),之后,你会看到两边带下划线和不带下划线的方法:
__方法__ 内置方法,可能有多种执行方法,至少一种
方法 非内置方法,只有一种执行方法,对象.方法
 
int例子:
  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__
  1. help(int)
  2. ....
  3. | __cmp__(...) | x.__cmp__(y) <==> cmp(x,y) |
  4. ....
  5.  
  6. >>> int("11",base=2)#base表示要转换为2进制
  7. 3
  8. >>> int("F",base=16)#base表示要转换为16进制
  9. 15
  10. >>> age=19
  11. >>> age.__cmp__(3)#19比3大,返回1
  12. 1
  13. >>> age.__cmp__(32)#19比32小,返回-1
  14. -1
  15. >>> age.__cmp__(19)#两个19比较,返回0
  16. 0
  17. >>> cmp(19,3)#19比3大,返回1
  18. 1
  19. >>> cmp(19,32)#19比32小,返回-1
  20. -1
  21. >>> cmp(19,19)#两个19比较,返回0
  22. 0
  23.  
  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,才够分页
  17.  
 
  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‘>#转换为浮点型
 
  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
 
  1. def __hash__(self):
  2. """如果对象object为哈希表类型,返回对象object的哈希值。哈希值为整数。在字典查找中,哈希值用于快速比较字典的键。两个数值如果相等,则哈希值也相等。"""
  3. """ x.__hash__() <==> hash(x) """
  4. pass
  5. >>> a="dfdsaufsuhadhfsaduhfsadfsdfdfsdafhsduifhsuidhfusdhfhsdufhsdfuhsdifisdjfiosdjfsdjfdsfdjds"
  6. >>> a.__hash__()
  7. 5423373101089537918
    
  1. def __hex__(self):
  2. """ 返回当前数的 十六进制 表示 """
  3. """ x.__hex__() <==> hex(x) """
  4. pass
  5. >>> age
  6. 18
  7. >>> age.__hex__()
  8. ‘0x12‘
  1. def __oct__(self):
  2. """ 返回改值的 八进制 表示 """
  3. """ x.__oct__() <==> oct(x) """
  4. pass
  5. >>> age
  6. 18
  7. >>> age.__oct__()
  8. ‘022‘
  1. def __neg__(self):
  2. """ x.__neg__() <==> -x """
  3. pass
  4. >>> age
  5. 18
  6. >>> age.__neg__()
  7. -18
  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
  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
 
  1. def __index__(self):
  2. """ 用于切片,数字无意义 """
  3. """ x[y:z] <==> x[y.__index__():z.__index__()] """
  4. pass
  5. #为什么一个对象能切片啊,因为里面有index方法!
 
 





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

原文:http://www.cnblogs.com/binhy0428/p/5125938.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!