zoukankan      html  css  js  c++  java
  • python的collection系列-可命名元组(namedtuple)

     1 import collections
     2 #创建类
     3 MytupleClass = collections.namedtuple("MytupleClass",["x","y","z"])
     4 obj = MytupleClass(11,22,33)
     5 print(obj.x)
     6 print(obj.y)
     7 print(obj.z)     #应用于坐标
     8 #print(help(MytupleClass))     #查看创建的类有哪些功能
     9 
    10 #执行结果:
    11 11
    12 22
    13 33

    源码:

      1 class MytupleClass(builtins.tuple)
      2  |  MytupleClass(x, y, z)
      3  |  
      4  |  Method resolution order:
      5  |      MytupleClass
      6  |      builtins.tuple
      7  |      builtins.object
      8  |  
      9  |  Methods defined here:
     10  |  
     11  |  __getnewargs__(self)
     12  |      Return self as a plain tuple.  Used by copy and pickle.
     13  |  
     14  |  __getstate__(self)
     15  |      Exclude the OrderedDict from pickling
     16  |  
     17  |  __repr__(self)
     18  |      Return a nicely formatted representation string
     19  |  
     20  |  _asdict(self)
     21  |      Return a new OrderedDict which maps field names to their values.
     22  |  
     23  |  _replace(_self, **kwds)
     24  |      Return a new MytupleClass object replacing specified fields with new values
     25  |  
     26  |  ----------------------------------------------------------------------
     27  |  Class methods defined here:
     28  |  
     29  |  _make(iterable, new=<built-in method __new__ of type object at 0x684357D0>, len=<built-in function len>) from builtins.type
     30  |      Make a new MytupleClass object from a sequence or iterable
     31  |  
     32  |  ----------------------------------------------------------------------
     33  |  Static methods defined here:
     34  |  
     35  |  __new__(_cls, x, y, z)
     36  |      Create new instance of MytupleClass(x, y, z)
     37  |  
     38  |  ----------------------------------------------------------------------
     39  |  Data descriptors defined here:
     40  |  
     41  |  __dict__
     42  |      A new OrderedDict mapping field names to their values
     43  |  
     44  |  x
     45  |      Alias for field number 0
     46  |  
     47  |  y
     48  |      Alias for field number 1
     49  |  
     50  |  z
     51  |      Alias for field number 2
     52  |  
     53  |  ----------------------------------------------------------------------
     54  |  Data and other attributes defined here:
     55  |  
     56  |  _fields = ('x', 'y', 'z')
     57  |  
     58  |  _source = "from builtins import property as _property, tupl..._itemget...
     59  |  
     60  |  ----------------------------------------------------------------------
     61  |  Methods inherited from builtins.tuple:
     62  |  
     63  |  __add__(self, value, /)
     64  |      Return self+value.
     65  |  
     66  |  __contains__(self, key, /)
     67  |      Return key in self.
     68  |  
     69  |  __eq__(self, value, /)
     70  |      Return self==value.
     71  |  
     72  |  __ge__(self, value, /)
     73  |      Return self>=value.
     74  |  
     75  |  __getattribute__(self, name, /)
     76  |      Return getattr(self, name).
     77  |  
     78  |  __getitem__(self, key, /)
     79  |      Return self[key].
     80  |  
     81  |  __gt__(self, value, /)
     82  |      Return self>value.
     83  |  
     84  |  __hash__(self, /)
     85  |      Return hash(self).
     86  |  
     87  |  __iter__(self, /)
     88  |      Implement iter(self).
     89  |  
     90  |  __le__(self, value, /)
     91  |      Return self<=value.
     92  |  
     93  |  __len__(self, /)
     94  |      Return len(self).
     95  |  
     96  |  __lt__(self, value, /)
     97  |      Return self<value.
     98  |  
     99  |  __mul__(self, value, /)
    100  |      Return self*value.n
    101  |  
    102  |  __ne__(self, value, /)
    103  |      Return self!=value.
    104  |  
    105  |  __rmul__(self, value, /)
    106  |      Return self*value.
    107  |  
    108  |  __sizeof__(...)
    109  |      T.__sizeof__() -- size of T in memory, in bytes
    110  |  
    111  |  count(...)
    112  |      T.count(value) -> integer -- return number of occurrences of value
    113  |  
    114  |  index(...)
    115  |      T.index(value, [start, [stop]]) -> integer -- return first index of value.
    116  |      Raises ValueError if the value is not present.
  • 相关阅读:
    win10彻底永久关闭自动更新的方法
    kibana.yml配置
    完整记录安装elasticsearch的过程
    docker下nginx的安装
    centos7删除mysql
    21 | panic函数、recover函数以及defer语句 (上)
    07 | 数组和切片
    SNAPSHOT包上传nexus成功,下载失败
    extract method(提炼函数)
    枚举中不要再出现数字了
  • 原文地址:https://www.cnblogs.com/repo/p/5423221.html
Copyright © 2011-2022 走看看