#__author__ = 'DouYunQian' #coding = utf - 8 class Foo (): def __init__ ( self , name , age ): self . name = name self . age = age def __getitem__ ( self , item ): #特殊成员 print ( "getItem" ) print ( item ) print ( type ( item )) #sli
#coding=utf-8
class Foo():
def __init__(self,name,age):
self.name=name
self.age=age
def __getitem__(self,item):#特殊成员
print("getItem")
print(item)
print(type(item))#slice对象
print(item.start)#开始的数字
print(item.stop)#最后的数字
print(item.step)#步长
def __setitem__(self,key,value):#特殊成员
print("setitem")
def __delitem__(self,key):#删除函数
print("delitem")
obj=Foo("alex",78)
obj[2:5:2]
del obj["fsd"]
#__author__ = 'DouYunQian'
#coding=utf-8
class Foo():
def __init__(self,name,age):
self.name=name
self.age=age
def __getitem__(self,item):#特殊成员
print("getItem")
print(item)
print(type(item))#slice对象
print(item.start)#开始的数字
print(item.stop)#最后的数字
print(item.step)#步长
def __setitem__(self,key,value):#特殊成员
print(type(key),type(value))
print("setitem")
def __delitem__(self,key):#删除函数
print(type(key))
print("delitem")
obj=Foo("alex",78)
obj[2:5:2]
obj[9:6]=555
del obj[1:4]
输出:
getItem
slice(2, 5, 2)
<class 'slice'>
2
5
2
<class 'slice'> <class 'int'>
setitem
<class 'slice'>
delitem