mmdbcn 发表于 2017-4-23 11:53:14

python扫盲1

  yield可以用来实现generator
  next()和send(msg)的返回值是yield的参数。
  send(msg)的时候msg就作为对应yield返回值,
  例如t = yield 22,t就为msg。
  self.throw(GeneratorExit)
  人工终止generator
  is和==的区别
  python中每个对象有3个属性id、type、value
  is是id(对象的唯一标识,引用机制)的比较
  ==是value的比较
  pass
  没有啥都没有,用这个是为了代码美观、统一
  with obj as f:
  do sth.
  OBJ必须有__enter__和__exit__方法
  然后这个控制语句就相当于
  obj.__enter__(..)
  do sth.
  obj.__exit__(..)
  dir,str,type参数可以是python中所有对象
  a = 1
  dir(a)
  ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', 
  '__floordiv__', '__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__', '__str__', '__sub__', '__truediv__', '__xor__']
  查看内置属性和方法
  hasattr(a, '__abs__') == True
  >>> a = 12
  >>> print getattr(a, "__mod__")(5)
  2
  str(type(a))
  "<type 'int'>"
  >>> str(type(a))
  "<type 'int'>"
  >>> import types
  >>> print types.IntType
  <type 'int'>
  >>> print types.IntType == type(a)
  True
  >>> type(type(a))
  <type 'type'>
  最后比较有趣的是type自己解释了自己
页: [1]
查看完整版本: python扫盲1