Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)

by LanX (Bishop)
on Apr 24, 2013 at 04:19 UTC ( #1030269=note: print w/replies, xml ) Need Help??


in reply to Re^3: RFC: Simulating Ruby's "yield" and "blocks" in Perl
in thread RFC: Simulating Ruby's "yield" and "blocks" in Perl

> FWIW, the equivalent in idiomatic Python3 is:

Thanks, interesting idea to turn it inside out! =)

But why Python3, AFAIK generators were already introduced in 2.

> while back a monstrosity was released...

Thanks, even more fun! =)

Especially the last line (after "bugs and limitations")

Oh, and almost forgot: We are sorry, Guido. So sorry.

Grin! xD

Cheers Rolf

( addicted to the Perl Programming Language)

update

) while its a bit cumbersome that you need to iterate over the yields... ... why not using a decorator to abstract the loop away?

update

this worked for me in Py 2.5.2

>>> def test(): ... print("You are in the method") ... yield 1 ... print("You are again back to the method") ... yield 2 ... >>> for a in test(): ... print("You are in the block "+str(a)) ... You are in the method You are in the block 1 You are again back to the method You are in the block 2
  • Comment on Re^4: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)
  • Download Code

Replies are listed 'Best First'.
Re^5: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)
by MonkOfAnotherSect (Sexton) on Apr 26, 2013 at 10:40 UTC
    v3 is current, though str.format() was added in late v2 releases for compatibility (printf style interpolation is to be deprecated some time but I'll use it below as an example...). Here's the decorated version as suggested:
      > I don't really see a win with this style, though, over giving test a parameter, and just using standard function calls (I had a little play with coroutines on the way but that's really overkill for this).

      Well I expected decorators to be more flexible, i.e. to redefine test() by putting a wrapper around it and not to built a new decorator @test (!?!)

      But I'm not too proficient with Python, and I'm misunderstanding the code.

      (well, there must be a reason why I prefer Perl's syntactic flexibility. ;-)

      I suppose def _(a) is a way to workaround the limitations of lamda?

      Is the name _ special (magic) or is it just a convention?

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030269]
help
Chatterbox?
[Corion]: WTF? There is a dev release of Danga::Socket?! I thought that module died a long time ago?! According to its changes, the last release was in 2008 ;)
[Corion]: (and Brad Fitzpatrick long since moved on to Go ;) )
[Corion]: But who am I to worry about necrocpan - I'm revisiting my online banking module, and also I'll somewhat revive WWW::Mechanize:: Firefox, at least if the test suite passes under the Waterfox browser ...
[Corion]: Maybe I should look through my CPAN releases and revisit them in order of last release and think about what to do with the modules :)
[marto]: this cpan day there should be a call to remove old crud :P
[marto]: as well as encourage active development

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-07-17 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (359 votes). Check out past polls.

    Notices?