Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90266
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <skip.montanaro@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'true,': 0.05; 'definitions.': 0.07; 'transform': 0.07; 'variables': 0.07; 'attributes': 0.09; 'happen.': 0.09; 'logic': 0.09; 'subject:instance': 0.09; 'subject:method': 0.09; 'def': 0.12; 'wrote': 0.14; '"called"': 0.16; '(0,': 0.16; '1):': 0.16; 'accepts': 0.16; 'attribute.': 0.16; 'branch:': 0.16; 'clause.': 0.16; 'cleaner': 0.16; 'csv': 0.16; 'first:': 0.16; 'func': 0.16; 'func_globals': 0.16; "function's": 0.16; 'globals': 0.16; 'globals.': 0.16; 'length,': 0.16; 'loop.': 0.16; 'preserve': 0.16; 'script.)': 0.16; 'subject:class': 0.16; 'subject:which': 0.16; 'user-defined': 0.16; 'applies': 0.16; 'appropriate': 0.16; '(not': 0.18; 'variable': 0.18; '8bit%:5': 0.22; '>>>': 0.22; 'input': 0.22; 'tests': 0.22; 'print': 0.22; '>>>': 0.24; 'instance,': 0.24; 'skip': 0.24; 'skip:% 10': 0.24; 'question': 0.24; 'sort': 0.25; 'script': 0.25; 'this:': 0.26; 'pass': 0.26; 'certain': 0.27; 'function': 0.29; 'raise': 0.29; 'thus': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'about.': 0.31; 'long.': 0.31; 'post.': 0.31; 'produces': 0.31; 'file': 0.32; 'class': 0.32; 'skip:c 30': 0.32; 'stuff': 0.32; 'run': 0.32; 'another': 0.32; 'text': 0.33; 'plain': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; "can't": 0.35; 'classes': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; "didn't": 0.36; 'method': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'skip:& 10': 0.38; 'requiring': 0.38; 'rich': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'short': 0.38; 'skip:& 20': 0.39; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'system.': 0.39; 'called': 0.40; '8bit%:6': 0.40; 'how': 0.40; 'even': 0.60; 'chain': 0.60; 'break': 0.61; 'challenge': 0.61; 'new': 0.61; 'simple': 0.61; 'first': 0.61; 'here:': 0.62; 'real': 0.63; 'skip:n 10': 0.64; 'decided': 0.64; 'more': 0.64; 'occur': 0.65; 'situation': 0.65; 'world': 0.66; 'gathering': 0.68; 'repeat': 0.74; 'potentially': 0.81; 'discovered': 0.83; 'end.': 0.84; 'nowadays,': 0.84; 'esteemed': 0.91; 'before?': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=YbOyFxYwCVHQMQPqe8tub6dZ2pMGTToc2XzXKTUzOLo=; b=cJHKHgiv8BaJGuW1RImKhM7pAGS0h+XfblODCiH6OAguBkk2NAXEo4DfYDLfTvessx s+2Vt+jzz9opmolZu4fohof77e/92HplNvYDno3CSTNvBlM8sKp/vm69s7E86fGIx5Kg XPekeYcHXYRZ0KXiZqqM+vmJLaSnIGyiKwIIkgIE2Lga0TBTdN0viL4yiwbtLbywB3Gl pmUMlXUdWzdUMgT1PdcEzuJesJkQlUklf2vP1Ez2znw+odt83EkN/FN2mAuuXidwjG6n b9nvfKj6zEFbNRvscqCbnwC5sefRJfUcYg6rVfGeFUXVWbl2r957ylEEQYMcYuKH3rfP BOJA== |
| MIME-Version | 1.0 |
| X-Received | by 10.202.193.8 with SMTP id r8mr2725773oif.27.1431087863260; Fri, 08 May 2015 05:24:23 -0700 (PDT) |
| Date | Fri, 8 May 2015 07:24:23 -0500 |
| Subject | A __call__ "method" which is itself another callable class instance? |
| From | Skip Montanaro <skip.montanaro@gmail.com> |
| To | Python <python-list@python.org> |
| Content-Type | multipart/alternative; boundary=001a113cd73cbb6d880515911b43 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.241.1431087871.12865.python-list@python.org> (permalink) |
| Lines | 202 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1431087871 news.xs4all.nl 2896 [2001:888:2000:d::a6]:37203 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:90266 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
This is just a pedantic post. It has no real useful application, just
something to think about. I apologize that the setup is rather long. The
stuff to think about is at the end. (I also apologize for using rich
text/html. In this HTML world in which we live nowadays, I'm never certain
that mail systems preserve leading whitespace, even in plain text mail.)
I have a script which accepts a CSV file as input and the definition of a
transform function. It applies the transform function to each row of the
CSV file and produces a new CSV file on standard out.
One of the things I discovered I needed in certain circumstances was to
inject user-defined variables into the system. Since it's a single pass
sort of thing (not long-lived), I decided to inject those variables into
the function's globals. (I could have added them to builtins, I suppose,
but it didn't occur to me when I first wrote the script.)
The challenge was to find the appropriate globlas dictionary into which to
inject these variable definitions. My code wound up looking like this:
if inspect.ismethod(func):
func_globals = func.im_func.func_globals
elif inspect.isfunction(func):
func_globals = func.func_globals
elif inspect.isbuiltin(func):
func_globals = sys.modules[func.__module__].__dict__
elif inspect.ismethoddescriptor(func):
raise TypeError("Can't get globals of a method descriptor")
What if func is actually an instance of a class with a __call__ method? So
I added another branch:
elif hasattr(func, "__call__"):
func = func.__call__
But now what? This required me to to start the process over again. I also
needed an else clause. Let's add that first:
else:
raise TypeError("Don't know how to find globals from %s objects" %
type(func))
I need to repeat the tests in the case where I found a non-function-like
object with a __call__ attribute. I decided to wrap this logic in a for
loop which could only run twice:
for _i in (0, 1):
if inspect.ismethod(func):
func_globals = func.im_func.func_globals
elif inspect.isfunction(func):
func_globals = func.func_globals
elif inspect.isbuiltin(func):
func_globals = sys.modules[func.__module__].__dict__
elif inspect.ismethoddescriptor(func):
raise TypeError("Can't get globals of a method descriptor")
elif hasattr(func, "__call__"):
func = func.__call__
# Try again...
continue
else:
raise TypeError("Don't know how to find globals from %s
objects" % type(func))
break
else:
raise TypeError("Don't know how to find globals from %s objects" %
type(func))
I thought about using while True, but thought, "nah, that's never going to
happen." Then I wondered if it *could* happen. I constructed a short chain
of __call__ attributes which might require the loop to repeat more than
twice, but I can't think of a situation where it would actually be useful:
class C1(object):
def __call__(self):
print "called"
class C2(object):
__call__ = C1()
If you instantiate C2 and call the instance, it works as you would expect:
>>> o = C2()
>>> o()
called
>From that simple example, it's straightforward to create a chain of classes
of any length, thus requiring my for loop to potentially be a while True
loop. The question for the esteemed gathering here: have you ever
encountered the need for this class-instance-as-a-dunder-call-method
before? Perhaps in some sort of code generation situation? Or cleaner
functions-with-attributes?
Skip
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
A __call__ "method" which is itself another callable class instance? Skip Montanaro <skip.montanaro@gmail.com> - 2015-05-08 07:24 -0500
csiph-web