Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def the_gen():
- i = 0
- while True:
- print "the_gen called", i
- yield i
- i += 1
- def do(it):
- n = 0
- for i in it:
- print "work on",i
- n += 1
- if n > 10:
- break
- def function(input):
- """
- Take an input that may be a iterable or not.
- """
- iterable = None
- if isinstance(input, basestring):
- """ basestring is the only particular case as it is also an iterable"""
- iterable = (input, )
- else:
- try:
- iterable = (x for x in input)
- except TypeError:
- iterable = (input, )
- if iterable is not None:
- do(iterable)
- gen = the_gen()
- print "* generateur infini"
- function(gen)
- print "\n\n* string"
- function("bla bla bla")
- print "\n\n* string split"
- function("bla bla bla".split())
- print "\n\n* list comprehension (bad)"
- function([gen.next() for x in xrange(100)])
- print "\n\n* generator (good)"
- function((gen.next() for x in xrange(100)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement