Python dynamic attribute creation
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Thu Jul 1 21:10:47 EDT 2010
On Thu, 01 Jul 2010 23:46:55 +0800, WANG Cong wrote:
> However, I think setattr() is a builtin function, using it exposes the
> *magic* of metaprogramming (or class-programming, if more correct) at a
> first glance.
There's nothing magic about metaprogramming.
If you're a programmer, you write programs. Some of those programs will
be programs about text ("programming"), programs about numbers
("programming"), programs about databases ("programming"), programs about
staff performance data ("programming"), and programs about programs
("metaprogramming"). But it's still just programming, and there's nothing
magical about it. Languages that try to frighten you away from
metaprogramming are doing you a disservice. That's like teaching an
electrical engineer how to make all sorts of devices, but not how to make
voltmeters or ammeters because they're "meta-electronics".
I'm pretty sure that IT is the only discipline that cares about this
distinction between "normal work" and "meta work". Engineers are quite
happy to make the tools they need to make the tools they need to make the
tools they need to make something. Carpenters would think you were crazy
if you said that building a scaffold was "meta-carpentry" and therefore
"magic" and something to be avoided.
There's even a children's song about the sort of loops you can get into
when you need a tool X to fix some item Y, but you need Y in order to fix
the tool first:
http://www.songsforteaching.com/folk/theresaholeinthebucket.htm
It's only in IT that "meta work" is considered "special", and that (I
think) is because of the influence of academia. Don't get me wrong, I'm a
big fan of academia, but I think sometimes they need a good dose of
reality.
Back before IT was a discipline, people used to write self-modifying code
and programs to generate programs and other metaprogramming all the time,
and nobody thought twice about it. Now admittedly, *some* metaprogramming
techniques (like self-modifying code) make debugging much, much harder,
and therefore should be avoided *for that reason*, but that's nothing to
do with metaprogramming. If you write code with variables like:
a1649264273528924 = a1649269279528924 + a1645976427328924
that's just "programming" but it's damn near impossible to efficiently
debug too.
--
Steven
More information about the Python-list
mailing list