What is Python good for?
sam at localhost.localdomain
Sat Sep 15 03:06:39 CEST 2001
"Ignacio Vazquez-Abrams" <ignacio at openservices.net> wrote in message
> Here's something that came up recently that if it doesn't make you go
> "ooh-ahh", then it will make you go "whoa":
> >>> def a():
> ... return 'abc'
> >>> def b():
> ... return 123
> >>> a.func_code=b.func_code
Max M adds:
> wouldn't a = b satisfy your need for whoas here ?
It's perhaps a little easier to see the potential usefulness of the
instance version of this Stupid Python Trick:
>>> class A:
... def f(self):
... print 'calling A.f'
>>> class B:
... def g(self):
... print 'calling B.g'
>>> a = A()
>>> b = B()
>>> a.f = b.g
In other words, you can dynamically and arbitrarily mix and match
instance methods. How does this demonstrate Python's wonderfulness?
1) You can do "paradigm-breaking" things like this with such little
fuss. It doesn't get much simpler than "a.f = b.g" No new keywords, no
2) There are no (?) common cases where the language's governing paradigm
forces you to do something this arbitrary (a counterexample for Java
might be the fact that "Hello World" is a class definition invoking half
a dozen keywords whose name is the same as the file containing it).
3) Because of 2), the culture of the language guides you away from these
tricks in the common cases. This is a big deal; it helps us understand
each other's dialects of Python. In other words, it helps you read that
library module you need to import and helps newbies and *bots feel like
they are engaged in a common enterprise. (Perl is arguably the crowning
jewel of the opposite philosophy: every Stupid Trick is worth trying.)
4) When you do roll your own Stupid Python Trick, Python will often
astonish you with the robustness of the results. If they work at all,
they will usually just work.
More information about the Python-list