Prev: Why doesn't python's list append() method return the list itself?
Next: Getting started with python on macintosh snow leopard with mysql - need help
From: Thomas Jollans on 23 Jul 2010 14:07 On 07/23/2010 07:13 PM, Thomas Jollans wrote: > On 07/23/2010 12:34 AM, wheres pythonmonks wrote: >> 2. Is there a better way to loopup by id? I'm not very familiar with >> sys.exc_info, but creating the id->name hash each time seems like >> overkill. > > I just had the most horrendous idea. Really, looking up objects by ID, > or even swapping two objects, isn't that difficult if you do some C > black magic. Don't try this in front of the kids. > > I wrote a little module, called "hell": > > Python 3.1.2 (release31-maint, Jul 8 2010, 09:18:08) > [GCC 4.4.4] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> >>>> from hell import swap, getptr >>>> dir > <built-in function dir> >>>> len > <built-in function len> >>>> swap(dir, len) >>>> dir > <built-in function len> >>>> len > <built-in function dir> >>>> a = "this was a" >>>> b = "this was b, hell yeah" >>>> (a, b, id(a), id(b)) > ('this was a', 'this was b, hell yeah', 32417752, 32418144) >>>> tpl = (a, b, id(a), id(b)) >>>> tpl > ('this was a', 'this was b, hell yeah', 32417752, 32418144) >>>> swap(a, b) >>>> tpl > ('this was b, hell yeah', 'this was a', 32417752, 32418144) >>>> getptr(32417752) > 'this was b, hell yeah' >>>> The great thing about this is that it can illustrate, in a very perverse manner, some lovely facets of Python. For example: combined hash and equality checks when accessing sets and dicts: Python 3.1.2 (release31-maint, Jul 8 2010, 09:18:08) [GCC 4.4.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from hell import swap >>> s1 = 'string1' >>> s2 = 'string2' >>> s = {s1, s2} >>> swap(s1, s2) >>> s {'string1', 'string2'} >>> s1 in s False >>> s2 in s False >>> s1 in list(s) True >>> s2 in list(s) True >>>
From: Terry Reedy on 23 Jul 2010 18:08 On 7/23/2010 4:10 AM, Steven D'Aprano wrote: > Using exec or friends to avoid the overhead of function calls is like > pushing your car to work to avoid the overhead of having to get in and > out of the car. And, of course, exec *is* a function call (explicitly in 3.x, but somewhere also in the innards of 2.x). Thanks for the laugh of the day. -- Terry Jan Reedy
From: Albert van der Horst on 6 Aug 2010 15:41
In article <4c495b50$0$28634$c3e8da3(a)news.astraweb.com>, Steven D'Aprano <steve(a)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(a)spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst |