Easy questions from a python beginner

Albert van der Horst albert at spenarnc.xs4all.nl
Fri Aug 6 15:41:28 EDT 2010


In article <4c495b50$0$28634$c3e8da3 at news.astraweb.com>,
Steven D'Aprano  <steve at REMOVE-THIS-cybersource.com.au> wrote:
>On Thu, 22 Jul 2010 21:23:05 -0700, Stephen Hansen wrote:
>
>> On 7/22/10 7:47 PM, wheres pythonmonks wrote:
>[...]
>>> The truth is that I don't intend to use these approaches in anything
>>> serious.  However, I've been known to do some metaprogramming from time
>>> to time.
>>
>> Depending on how you define "metaprogramming", Python is pretty
>> notoriously ill-suited towards the task (more, its basically considered
>> a feature it doesn't let you go there) -- or, it allows you to do vast
>> amounts of stuff with its few dark magic hooks. I've never had a
>> satisfying definition of metaprogramming that more then 50% of any group
>> agree with, so I'm not sure which you're looking for. :)
>
>I disagree strongly at your characterisation that Python is notorious for
>being ill-suited towards metaprogramming. I'd say the complete opposite
>-- what is considered dark and scary metaprogramming tasks in other
>languages is considered too ordinary to even mention in Python.
>
>If you have a class that defines a single member (or attribute in Python
>terminology) "spam", and you want to add a second "ham" to a specific
>instance, such a thing is either deep, dark metaprogramming in some
>languages, if not outright impossible. In Python it is not even
>noteworthy:
>
>instance.ham = "something"  # *yawns*
>
>Recently there was a thread started by some Java or C++ refugee who was
>distressed about attribute access in Python, because it made
>metaprogramming frighteningly easy:
>
>http://mail.python.org/pipermail/python-list/2010-June/1248029.html
>
>My response at the time was: Python makes metaprogramming *easy*:
>
>http://mail.python.org/pipermail/python-list/2010-June/1248053.html
>
>I can't imagine how he would have reacted if we had showed him how easy
>it is to monkey-patch built-in functions...
>
>[...]
>> But! What it doesn't let you do is get clever with syntax and write a
>> sort of "simplified" or domain specific language to achieve certain
>> sorts of repetitive tasks quickly. You always end up writing normal
>> Python that looks like all other Python.
>
>Exactly... 90% of the time that you think you want a DSL, Python beat you
>to it.
>
>It is true that other languages (like Lisp, or Forth) allow you to write
>your own syntax. That's incredibly powerful, but it also has serious
>costs. If you can define your own syntax, that means every piece of code
>you look at is potentially a different language.

The answer is, of course, discipline.
I'm a Forth programmer and I morf Forth into an assembler.
Now some sources become assembler sources and one should be
very adamant about it. I've written a Perl interpreter for
those sources, and a Python interpreter would be even easier.

If I'm doing Python, I don't miss macro possibilities, but
sometimes my code gets repetitive.
If I ever get bored by repetition, there is m4.

m4 < aap.pym > aap.py
python aap.py

(See my site below for Forth assemblers, and non-trivial m4 usage.)

>--
>Steven

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst




More information about the Python-list mailing list