Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2360
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <kb1pkl@aim.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'method,': 0.07; 'sep': 0.07; 'python': 0.07; 'foo': 0.09; 'api': 0.11; '>>>': 0.12; 'def': 0.13; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '(yes': 0.16; '[gcc': 0.16; 'callable,': 0.16; 'ceval.c.': 0.16; 'from:addr:kb1pkl': 0.16; 'from:name:corey richardson': 0.16; 'linux2': 0.16; 'message-id:@aim.com': 0.16; 'richardson': 0.16; 'appear': 0.19; '(which': 0.21; 'skip:{ 10': 0.23; 'objects': 0.24; 'memory': 0.24; 'version': 0.25; 'class': 0.29; "python's": 0.29; 'implement': 0.30; 'does': 0.31; 'anyone': 0.31; 'to:addr:python-list': 0.32; "i've": 0.33; 'received:192': 0.34; 'received:192.168.0': 0.35; 'print': 0.35; 'header:User-Agent:1': 0.35; 'enough': 0.37; 'else': 0.37; 'received:192.168': 0.37; 'playing': 0.38; 'but': 0.38; 'happens': 0.38; 'speak': 0.39; 'ok,': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'subject: (': 0.39; 'stopped': 0.40; 'would': 0.40; 'subject:The': 0.69; '__call__': 0.84; 'ate': 0.84; 'received:172.29.41': 0.84 |
| Date | Fri, 01 Apr 2011 11:07:15 -0400 |
| From | Corey Richardson <kb1pkl@aim.com> |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | The Magick of __call__ (Or, Digging Deeper Than I Ought To) |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| x-aol-global-disposition | G |
| X-AOL-SCOLL-SCORE | 0:2:408434432:93952408 |
| X-AOL-SCOLL-URL_COUNT | 0 |
| x-aol-sid | 3039ac1d29444d95ea250d5b |
| X-AOL-IP | 71.181.115.87 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.77.1301670452.2990.python-list@python.org> (permalink) |
| Lines | 42 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1301670453 news.xs4all.nl 41103 [::ffff:82.94.164.166]:57226 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:2360 |
Show key headers only | View raw
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