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())
到此这篇关于python迭代器自定义类的具体方法的文章就介绍到这了,更多相关python迭代器怎样自定义类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!