class example(object):
def __init__(self,num):
self.num=num
def __iter__(self):
return self
def __next__(self):
if self.num = 0:
raise StopIteration
tmp = self.num
self.num -= 1
return tmp
a = example(3)
print(a.__next__())
print(a.__next__())
print(a.__next__())
print(a.__next__())
class Person:
def __init__(self, first, middle, last, age):
self.first = first;
self.middle = middle;
self.last = last;
self.age = age;
def __str__(self):
return self.first + ' ' + self.middle + ' ' + self.last + \
' ' + str(self.age)
def initials(self):
return self.first[0] + self.middle[0] + self.last[0]
def changeAge(self, val):
self.age += val
myPerson = Person('Raja', 'I', 'Kumar', 21)
print(myPerson)
myPerson.changeAge(5)
print(myPerson)
print(myPerson.initials())
到此這篇關(guān)于python迭代器自定義類的具體方法的文章就介紹到這了,更多相關(guān)python迭代器怎樣自定義類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!