Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10846
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ericsnowcurrently@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'subject:object': 0.07; 'attribute': 0.09; 'defined,': 0.09; 'derived': 0.09; 'namespace': 0.09; 'object.': 0.09; 'defined': 0.14; 'definition,': 0.16; 'definition.': 0.16; 'f()': 0.16; 'frame,': 0.16; 'ideas?': 0.16; 'module:': 0.16; 'settle': 0.16; 'class,': 0.16; 'classes,': 0.16; 'def': 0.16; 'functions,': 0.19; 'to:name:python-list': 0.19; '(like': 0.21; "doesn't": 0.22; 'trying': 0.23; 'module,': 0.23; 'received:209.85.215.46': 0.23; 'received:mail-ew0-f46.google.com': 0.23; 'thus': 0.23; 'code': 0.24; 'stack': 0.25; 'modules': 0.25; 'skip:[ 10': 0.26; 'function': 0.26; "i'm": 0.27; 'knowing': 0.28; 'objects': 0.28; 'message-id:@mail.gmail.com': 0.28; 'bound': 0.29; 'object': 0.30; 'module': 0.30; 'nested': 0.30; 'researching': 0.30; 'class': 0.31; 'shared': 0.32; 'to:addr:python-list': 0.34; 'there': 0.34; 'however,': 0.34; 'frame': 0.35; 'yet,': 0.35; 'pretty': 0.35; 'question': 0.35; 'similar': 0.37; 'bigger': 0.37; 'functions.': 0.37; 'some': 0.37; 'but': 0.37; 'using': 0.37; 'another': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'holding': 0.38; 'either': 0.39; 'under': 0.39; 'to:addr:python.org': 0.39; 'determine': 0.40; 'received:209': 0.40; '"":': 0.84; 'easy:': 0.84; 'lastly,': 0.84; 'running,': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=dS7OHkrbVZR5rLxpsruvU+pKJ2HvHTqEIIr4LPTBxCc=; b=aoTplKDTv2rP2ex6L6haaB0mZzYTkQPcY+h01ylXaBnLmAmXRSQQj9k+26zNSwCCyK eNX2WnYVBKNa3bSfYN+zJeBE57W/QcEiY/7bly+fRpJMQr/i1fB2D/XLAwAp1k6y5Yhg 8FsrOiLEJOzRwauXS3YEdlo3g7blg6YUkAkw8= |
| MIME-Version | 1.0 |
| Date | Thu, 4 Aug 2011 01:49:39 -0600 |
| Subject | finding the object corresponding to a stack frame |
| From | Eric Snow <ericsnowcurrently@gmail.com> |
| To | python-list <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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.1881.1312444181.1164.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1312444181 news.xs4all.nl 23886 [2001:888:2000:d::a6]:40261 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:10846 |
Show key headers only | View raw
For any given stack frame, the corresponding code object is derived
most immediately from either a module [definition], a class
definition, or function definition. I want to be able to determine
the specific module, class, or function object for any arbitrary
code/frame object. For modules it is pretty straightforward. For
classes, the object doesn't exist yet, but I can work around that.
Lastly, for functions it is not nearly as simple as I had hoped. If
the function is defined in the global namespace then the solution is
relatively trivial, using the inspect module:
def get_context():
frame = inspect.currentframe().f_back
if frame.f_code.co_name == "<module>":
return sys.modules[frame.f_locals["__name__"]]
return frame.f_globals.get(frame.f_code.co_name)
def f():
context = get_context()
f()
However, under other conditions it is not so easy:
- the function is nested inside another,
- the code object is shared between multiple functions,
- the function is accessed as an attribute of some other object and
thus is not bound by its name in the easy search namespaces (like
globals),
- others?
I'm trying to see if there is a way to work through these issues. It
would be great if objects knew under which object each was defined,
but that is a bigger question for another time. Right now I would
just settle for a frame object knowing the object for which it is
running, particularly for functions. However, judging by similar
questions found while researching this, I'm not holding my breath.
Any ideas?
-eric
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
finding the object corresponding to a stack frame Eric Snow <ericsnowcurrently@gmail.com> - 2011-08-04 01:49 -0600
csiph-web