Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72930

Re: How to use imported function to get current globals

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'cpython': 0.05; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'globals(),': 0.16; "module's": 0.16; 'normally,': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'import': 0.22; '(in': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'certainly': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'said,': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'this.': 0.32; 'probably': 0.32; 'but': 0.35; 'received:google.com': 0.35; 'how': 0.40; 'ian': 0.60; 'more': 0.64; 'here': 0.66; 'useful.': 0.68; 'subject:get': 0.81; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=pVaaEd+TKR1PaaRv0HAiN2loxWSyqS7TTpV6OwtAsZA=; b=JRN+VuNNeaKsOEpojpqUqn65dGWc0j76IKWXmjvQRGaZQ88Fk3zbeNE6aOuJa/89yW E8JQRJF7GUAGQIxE2qja0LNaFz+5ERpAj0LHgr8CtDuRvbXwcLWmY4iCj0gUVNWf/KAT if4weC+6JuFEnEoLMMuRoDjGYZv1lpggGVnp210Ma6wKhR7cluBDV+YjQRY4qgNebCo1 qQUWcKnY62GZQpPO/2Biv8bYLx+agQVuWAiwXfGclcE4FKme2OdC1iRk/3SDaBt7bu3E uHkv4U1kmXomll5EEq7D+4YFnpFsuRWD10X0kEEHcqN9oy9yc+qhwSYu7wYCgT7jyYcs WiLA==
MIME-Version 1.0
X-Received by 10.221.64.80 with SMTP id xh16mr15088092vcb.35.1402174321660; Sat, 07 Jun 2014 13:52:01 -0700 (PDT)
In-Reply-To <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com>
References <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com>
Date Sun, 8 Jun 2014 06:52:01 +1000
Subject Re: How to use imported function to get current globals
From Chris Angelico <rosuav@gmail.com>
Cc python_list <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.10861.1402174329.18130.python-list@python.org> (permalink)
Lines 20
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402174329 news.xs4all.nl 2950 [2001:888:2000:d::a6]:33528
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72930

Show key headers only | View raw


On Sun, Jun 8, 2014 at 3:40 AM, 1989lzhh <1989lzhh@gmail.com> wrote:
> Here is the code
> m1.py
> def f():
>     print globals()
>
> m2.py
> from m1 import f
> f()# how to get current module's globals?

As Ian said, you almost certainly do not want to do this. But if you
have a solid use-case that involves finding the caller's globals, you
can do it (in CPython - no idea about other Pythons) with the
backtrace.

Normally, passing dictionaries around is going to be MUCH more useful.
(And probably not actually globals(), you almost never want to use
that.)

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to use imported function to get current globals Chris Angelico <rosuav@gmail.com> - 2014-06-08 06:52 +1000

csiph-web