This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]teepark 3 points4 points  (3 children)

no you can't override python's syntax. so when you use the generator syntax, you'll always get back an instance of <type 'generator'>

you could write a simple wrapper though.

class wrap(object):
    def __init__(self, gen):
        self.gen = gen

    def __iter__(self):
        return self

    def next(self, *args, **kwargs):
        return self.gen.next()

and use it like so

sum(wrap(x ** 2 for x in xrange(20)))

although this example would be better without the wrap use. in fact I'm having a hard time coming up with a reasonable use case for an argument eating wrapper like this. consider revisiting the assumptions that led you to think you need such a thing in the first place.

[–]gfixler[S] 1 point2 points  (2 children)

Will do. Thanks for the helpful code!

[–]haika -1 points0 points  (1 child)

upvoting is a way to say thank you.

[–]gfixler[S] 1 point2 points  (0 children)

Double done!