Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89853

Re: code blocks

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.python
Subject Re: code blocks
Date 2015-05-03 11:56 +0100
Organization A noiseless patient Spider
Message-ID <87a8xm3p5h.fsf@bsb.me.uk> (permalink)
References <mailman.49.1430633046.12865.python-list@python.org>

Show all headers | View raw


"Dr. John Q. Hacker" <zondervanz@gmail.com> writes:

> I'm thinking how interesting it would be to add code blocks to Python,
> so that arbitrary strings of code can be passed around. It would open
> up some interesting possibilities for self-modifying code and generic
> programming.
>
> Since Python has already a plethora of ambiguous string designations,
> one of them could be set aside specificially for code blocks:
>
> """for i in n: print i"""
>
> For any variables, like "n", it would access the scope in which it was
> running. When you tried to print a triple-double-quoted code block,
> perhaps it could invoke the code.
>
> My suggestion would be to use triple double-quoted strings. 
>
> You probably already know that Ruby has code blocks.

What Ruby calls code blocks, Python calls lambdas:

  Ruby:  [1,2,3].map { |x| x*x }
  Python: map(lambda x: x*x, [1,2,3])

In some cases, the lambda is implicit.  For example, the above can be
written as

  [x*x for x in [1,2,3]]

You can use strings (if you really must), using eval() at the
appropriate place eval() but that opens up all sorts of cans of worms.

-- 
Ben.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

code blocks "Dr. John Q. Hacker" <zondervanz@gmail.com> - 2015-05-02 13:30 -0500
  Re: code blocks Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-03 11:56 +0100
  Re: code blocks zipher <dreamingforward@gmail.com> - 2015-05-10 18:39 -0700
    Re: code blocks Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-10 21:31 -0600
      Re: code blocks Rustom Mody <rustompmody@gmail.com> - 2015-05-10 20:40 -0700
      Re: code blocks zipher <dreamingforward@gmail.com> - 2015-05-11 08:22 -0700
        Re: code blocks zipher <dreamingforward@gmail.com> - 2015-05-11 08:23 -0700
        Re: code blocks Chris Angelico <rosuav@gmail.com> - 2015-05-12 01:40 +1000
        Re: code blocks Peter Otten <__peter__@web.de> - 2015-05-11 18:01 +0200
        Re: code blocks Chris Angelico <rosuav@gmail.com> - 2015-05-12 02:07 +1000
          Re: code blocks zipher <dreamingforward@gmail.com> - 2015-05-11 09:51 -0700
        Re: code blocks Peter Otten <__peter__@web.de> - 2015-05-11 18:59 +0200
    Re: code blocks Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-10 21:36 -0600

csiph-web