hi this is the corotuıne example writen in def

namelst=['Charles Darwin (1809-82)', "Darwin's principal works, The Origin of Species (1859)", 'and The Descent of Man (1871) marked a new epoch in our', 'understanding of our world and ourselves.  His ideas', "were shaped by the Beagle's voyage around the world in", '1831-36.']

####PART 1 *****####################
def rotation(s):
	lst=list(s)
	cnt=0
	while len(lst):
		yield lst[cnt]
		cnt=(cnt +1) % len(lst)

r=rotation(namelst)
## PART 2****########
for x in range(8):
	print "turning: %s" % r.next()














#and ı want to write ıt with class by usıng ınherit
#according to ı know ınherit method was used when we want to add some fonctonalıty to our class
#so purposely ı want to dıvede ıt ınto 2 those classes
#and ı am stuck totaly at ımplemendıng the whıle loop


class rotator(object):
	def __init__(self):
		self.name=list
		self.lst=[]
	def setname(self, m):
		self.name=m
	def t(self):
		return self.name






S=rotator()
S.setname(namelst)
S.t()


def continueof(rotator):
	def __init__(self):
		sirano.__init__(self)
		self.cnt=0
	def __str__(self):
		sirano.__init__(self)
		while len(self.name):
			yield self.name[self.cnt]
			self.cnt=(self.cnt+ 1) % len(self.name)
	def nexter(self):
		for x in range(5):
			print "turn: %s" % continueof.next()

d=continueof(namelst)

d.__str__()
>>> d.nexter()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'nexter'
>>> 
# somethıng went wrong.ı used wrong method probably
>> d.__str__()

'None' # out put ıs NONE so ıt means there ıs no bypass data to whıle loop


how can ı pass the data to ınherıt class(

Dani AI

Generated

The core problem is structural, not magical: the snippet posted mixed up a function and a class, used __str__ as if it were a generator, and even set self.name = list (the type) instead of storing the list instance. is correct that def continueof(rotator): defines a function, not a subclass. For a class-based converter you want one class to hold the data and another to implement the iterator protocol.

A minimal, clear pattern is a base container + a subclass that implements iteration. Example (Python 3; add next = __next__ for Python 2):

class BaseHolder(object):
    def __init__(self, items=()):
        self.items = list(items)

class CyclingIterator(BaseHolder):
    def __init__(self, items=()):
        super(CyclingIterator, self).__init__(items)
        self._index = 0

    def __iter__(self):
        return self

    def __next__(self):
        if not self.items:
            raise StopIteration
        value = self.items[self._index]
        self._index = (self._index + 1) % len(self.items)
        return value

Usage: create c = CyclingIterator(your_list), get an iterator with it = iter(c), and call next(it) repeatedly (or use for / itertools.islice to limit iterations). Do not put yield inside __str____str__ must return a string, not act as an iterator. Also, call the base __init__ properly (via super(...) or BaseHolder.__init__(self, ...)) to ensure the list is stored.

Troubleshooting checklist: change def ... to class ... for subclasses; initialize state in __init__; implement __iter__/__next__ (and next alias for Py2); avoid calling d.str() to run iteration — use iter() and next() or a for loop. This separates data storage from iteration logic and will behave predictably.

def continueof(rotator):
is a function, not a class.
You can't set it up like a class and use it like a class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.