Python 基础教程

Python 高级教程

Python 相关应用

Python 笔记

Python tuple 内置类

Python 内置类 Python 内置类


tuple是位于builtins模块中的内置类,无需import模块就可直接使用,其实所有的元组都是该tuple内置类对象实例。

tuple类构造函数

语法

def __init__(self, seq=()):

参数

  • tuple(),不传任何参数,表示创建一个空元组对象;
  • tuple(iterable),传一个可遍历的数据对象,创建一个每个遍历项作为数据项的元组对象;如果传的是一个元组对象,返回的复制该元组的对象;

tuple类的方法

tuple类count方法

def count(self, value):

tuple的count方法表示查找元素value在tuple对象中出现次数。

tuple类index方法

def index(self, value, start=None, stop=None):

tuple的index方法表示查找元素value在tuple中第一个索引值;此外可以通过start和stop参数指定搜索的起始索引点和结束索引点。

tuple类逻辑运算等特殊方法

def __add__(self, *args, **kwargs):
    """ Return self+value. """
    pass

def __contains__(self, *args, **kwargs):
    """ Return key in self. """
    pass

def __eq__(self, *args, **kwargs):
    """ Return self==value. """
    pass

def __getattribute__(self, *args, **kwargs):
    """ Return getattr(self, name). """
    pass

def __getitem__(self, *args, **kwargs):
    """ Return self[key]. """
    pass

def __getnewargs__(self, *args, **kwargs):
    pass

def __ge__(self, *args, **kwargs):
    """ Return self>=value. """
    pass

def __gt__(self, *args, **kwargs):
    """ Return self>value. """
    pass

def __hash__(self, *args, **kwargs):
    """ Return hash(self). """
    pass

def __iter__(self, *args, **kwargs):
    """ Implement iter(self). """
    pass

def __len__(self, *args, **kwargs):
    """ Return len(self). """
    pass

def __le__(self, *args, **kwargs):
    """ Return self<=value. """
    pass

def __lt__(self, *args, **kwargs):
    """ Return self<value. """
    pass

def __mul__(self, *args, **kwargs):
    """ Return self*value.n """
    pass

def __ne__(self, *args, **kwargs):
    """ Return self!=value. """
    pass

def __repr__(self, *args, **kwargs):
    """ Return repr(self). """
    pass

def __rmul__(self, *args, **kwargs):
    """ Return self*value. """
    pass