Skip to main content
deleted 109 characters in body
Source Link
Justin
  • 21.4k
  • 9
  • 68
  • 117

This is pretty easy (25 chars for f(), indentation is counted as 1):

def f():
    while 1:yield 0
for i in f():print "Hello"

Here's a morean interesting way of doing it (32 chars for class x, 46 not counting the print):

  
class x:__iter__=next=lambda s:0s
for i in x():print "Hello"

In the first case, we simply define a never-ending generator. In the second, we defineThis defines an iterable whose next method never raises a StopIteration

This is pretty easy (25 chars for f(), indentation is counted as 1):

def f():
    while 1:yield 0
for i in f():print "Hello"

Here's a more interesting way (32 chars for class x):

 
class x:__iter__=next=lambda s:0
for i in x():print "Hello"

In the first case, we simply define a never-ending generator. In the second, we define an iterable whose next method never raises a StopIteration

This is an interesting way of doing it (32 chars for class x, 46 not counting the print):

 
class x:__iter__=next=lambda s:s
for i in x():print "Hello"

This defines an iterable whose next method never raises a StopIteration

added 64 characters in body
Source Link
Justin
  • 21.4k
  • 9
  • 68
  • 117

This is pretty easy (25 chars for f(), indentation is counted as 1):

def f():
    while 1:yield 0
for i in f():print "Hello"

Here's a more interesting way (32 chars for class x):

class x:next=lambda s:0;__iter__=lambda__iter__=next=lambda s:s0
for i in x():
    print "Hello"

In the first case, we simply define a never-ending generator. In the second, we define an iterable whose next method never raises a StopIteration

This is pretty easy:

def f():
    while 1:yield 0
for i in f():print "Hello"

Here's a more interesting way:

class x:next=lambda s:0;__iter__=lambda s:s
for i in x():
    print "Hello"

In the first case, we simply define a never-ending generator. In the second, we define an iterable whose next method never raises a StopIteration

This is pretty easy (25 chars for f(), indentation is counted as 1):

def f():
    while 1:yield 0
for i in f():print "Hello"

Here's a more interesting way (32 chars for class x):

class x:__iter__=next=lambda s:0
for i in x():print "Hello"

In the first case, we simply define a never-ending generator. In the second, we define an iterable whose next method never raises a StopIteration

Source Link
Justin
  • 21.4k
  • 9
  • 68
  • 117

This is pretty easy:

def f():
    while 1:yield 0
for i in f():print "Hello"

Here's a more interesting way:

class x:next=lambda s:0;__iter__=lambda s:s
for i in x():
    print "Hello"

In the first case, we simply define a never-ending generator. In the second, we define an iterable whose next method never raises a StopIteration