Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2360
| Date | 2011-04-01 11:07 -0400 |
|---|---|
| From | Corey Richardson <kb1pkl@aim.com> |
| Subject | The Magick of __call__ (Or, Digging Deeper Than I Ought To) |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.77.1301670452.2990.python-list@python.org> (permalink) |
All callables (things you can foo(bar)) are really just objects that
implement the __call__ method, as far as I understand. Well then, that
would appear to make methods themselves callable, so let's do a little
playing around...
lavos@lavos ~ $ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
... def __call__(self, foo):
... self.name(foo)
... def name(self, bar):
... print "Name: {0}".format(bar)
...
>>> foo = Foo()
>>> foo("Me!")
Name: Me!
Ok, nothing out of the ordinary. But what happens if....
>>> foo.name.__call__("Corey")
Name: Corey
>>> eval("foo.name" + (".__call__" * 9001) + "('Corey')")
Name: Corey
>>> foo.name.__call__.__call__.__call__.__call__.__call__("Corey")
Name: Corey
>>> eval("foo.name" + (".__call__" * 100000000) + "('Corey')")
^C^Z
[1]+ Stopped python
(Which was then followed by a ps aux and a kill -9...it ate all my
memory and then moved on to my swap.)
I've been told that much magick lies in the CALL_FUNCTION bytecode, and
that if I really cared I could look at ceval.c. I don't speak enough C
or know enough of Python's C API for it to be of any use (yes I
looked!). Would looking at something such as PyPy's version of it be
good for me / does anyone else have insights?
--
Corey Richardson
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
The Magick of __call__ (Or, Digging Deeper Than I Ought To) Corey Richardson <kb1pkl@aim.com> - 2011-04-01 11:07 -0400
csiph-web