好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python中descriptor的详细介绍

本文给大家分Python 中的 descriptor相关知识,非常不错,具有参考借鉴价值,需要的朋友参考下吧

descr.__get__(self, obj, type=None) --> value
descr.__set__(self, obj, value) --> None
descr.__delete__(self, obj) --> None 
class RevealAccess(object):
  """A data descriptor that sets and returns values
    normally and prints a message logging their access.
  """
  def __init__(self, initval=None, name='var'):
    self.val = initval
    self.name = name
  def __get__(self, obj, objtype):
    print('Retrieving', self.name)
    return self.val
  def __set__(self, obj, val):
    print('Updating', self.name)
    self.val = val
>>> class MyClass(object):
...   x = RevealAccess(10, 'var "x"')
...   y = 5
...
>>> m = MyClass()
>>> m.x
Retrieving var "x"
10
>>> m.x = 20
Updating var "x"
>>> m.x
Retrieving var "x"
20
>>> m.y
5 

以上就是Python中descriptor的详细介绍的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python中descriptor的详细介绍的详细内容...

  阅读:39次