Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35604
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <chris@rebertia.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; 'anyway.': 0.04; 'essentially': 0.04; 'explicitly': 0.04; 'function,': 0.07; 'wrapper': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'object)': 0.09; 'subject:while': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '(the': 0.15; 'dec': 0.15; 'skip:p 40': 0.15; 'anyhow,': 0.16; 'called,': 0.16; 'dictionary).': 0.16; 'exploits': 0.16; 'hard-code': 0.16; 'naming': 0.16; 'skip:@ 20': 0.16; 'value"': 0.16; 'wrote:': 0.17; 'tim': 0.18; 'code.': 0.20; 'skip:p 30': 0.20; 'email addr:gmail.com>': 0.20; 'decorators': 0.22; 'defined': 0.22; 'cc:2**0': 0.23; '>': 0.23; 'this:': 0.23; 'thus': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:" 20': 0.26; 'separate': 0.27; 'functions.': 0.27; 'i.e.': 0.27; 'skip:> 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'dictionary': 0.29; 'name?': 0.29; 'skip:& 10': 0.29; 'skip:& 30': 0.30; 'thursday,': 0.30; 'function': 0.30; 'code': 0.31; 'december': 0.32; 'generally': 0.32; 'print': 0.32; 'defining': 0.33; 'like:': 0.33; 'agree': 0.34; 'received:google.com': 0.34; 'whatever': 0.35; 'adds': 0.35; 'awesome': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'bad': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'step': 0.39; 'google': 0.39; '&': 0.61; 'repeat': 0.62; 'skip:\xc2 10': 0.62; 'here': 0.65; '26,': 0.65; 'reply': 0.66; 'god': 0.66; '8bit%:100': 0.70; 'composing': 0.84; 'excessive': 0.84; "it'd": 0.84; 'sender:addr:chris': 0.84; 'why?': 0.84; 'abc': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=SBfBn13uCpYXidg6Cv2iJtI66xNgpZ65h+MCId5u4jk=; b=DWft++C+kpxgZoNs6I5FVy/ecaE1EZsEXzmmc+HiHgwsssOjzgwgiEVu/CqgTJQ032 qiVFpHDmyOXn7AJd8rBnA3k2MIWwzKE25JR+v8o58ixfkvDv4sVAZBmyP/5/MNmDfEXQ Pm+lEEDQWpAnhLVIFq2qDgVxtV+X0PaItIv8c= |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=SBfBn13uCpYXidg6Cv2iJtI66xNgpZ65h+MCId5u4jk=; b=El/XFcQweQvIWyZfgKnhKy1bH4nGu5LUK2Poh+lQo6CTAizNIAuznt4VCFnuDkjIJ+ Bi9zguwgM6rGRRFcNf9BSMKyv4PwIfExn+9Hruop3MRC0l2FAetpd7rAGsbuzvhsO8V/ T6e33/dSMu1KS4dijZtFshjd79LNzwUI3x6K0wgiYiCHgt/SWXD/BYUfkM+gZMJjyWY5 MLkpJ44lKFgjvMF7YFam7sktebri3WT6WpGHV2S8jbfQiK4j1ix7HbFlx7N+R7bsM6Yc 2wnFCMIjn25VWADzXBOV48llqFvP5r6obkZ5WnRqd7nuY+YPeb3FQVjU/A2z/FEwW3v8 7FVA== |
| MIME-Version | 1.0 |
| Sender | chris@rebertia.com |
| In-Reply-To | <c9189a65-74ee-4ea7-a4b6-33ece392ea8f@googlegroups.com> |
| References | <c9548d77-ccc3-4b47-b84b-9a9f0c2852ce@googlegroups.com> <0kknd8tbg7knqa1ng6igbj8u82mqb720oi@4ax.com> <c9189a65-74ee-4ea7-a4b6-33ece392ea8f@googlegroups.com> |
| Date | Thu, 27 Dec 2012 00:58:27 -0800 |
| X-Google-Sender-Auth | pJj-018eLvXRw-sdrUuOmVjgjFU |
| Subject | Re: Finding the name of a function while defining it |
| From | Chris Rebert <clp2@rebertia.com> |
| To | Abhas Bhattacharya <abhasbhattacharya2@gmail.com> |
| Content-Type | multipart/alternative; boundary=f46d0447a10f0ff66d04d1d1c034 |
| X-Gm-Message-State | ALoCoQlG7GKACK33D71stFnMxnwbPX+VAvlvMZ5eDOeB72VD8+mgy17MToDBC70WAX2KCDZ72yhS |
| Cc | Python <python-list@python.org> |
| 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 | <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.1337.1356598710.29569.python-list@python.org> (permalink) |
| Lines | 120 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1356598711 news.xs4all.nl 6946 [2001:888:2000:d::a6]:39304 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:35604 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Dec 26, 2012 11:55 PM, "Abhas Bhattacharya" <abhasbhattacharya2@gmail.com>
wrote:
>
> On Thursday, 27 December 2012 10:22:15 UTC+5:30, Tim Roberts wrote:
> > Abhas Bhattacharya <abhasbhattacharya2@gmail.com> wrote:
> >
[Oh god please stop/avoid using Google Groups with its godawful
reply-quoting style that adds excessive blank lines]
> > >While I am defining a function, how can I access the name (separately
as
> > >string as well as object) of the function without explicitly naming
> > >it(hard-coding the name)?
> > >For eg. I am writing like:
> >
> > >def abc():
> > > #how do i access the function abc here without hard-coding the
name?
> >
> > Why? Of what value would that be?
<snip>
> Because I have this situation:
> I have used a dictionary with "function_name":value pair in the top of
the code. Now when some function is called, I need to print the value
assigned to its name in the dictionary (the functions are defined after the
dictionary). Now there is only one bad way-around for me: I need to
hard-code the name in the function like this:
> def function_name():
> print(dict_name.get("function_name"))
> but ofcourse it is a bad thing to do because I have a lot of this type of
functions. It would be better if I can can use the same code for all of
them, because they are all essentially doing the same thing.
I agree with the general outline of Mitya's suggestion, i.e. refactor the
"print the associated value" step into a separate function, thus obviating
the self-reference issue; it'd be bad to repeat that code in each function
anyway.
Anyhow, here's a simple variation that exploits decorators (because they're
generally awesome & one of my favorite features):
def printing_name_beforehand(func):
def wrapper(*args, **kwargs):
print(the_dict.get(func.__name__))
return func(*args, **kwargs)
return wrapper
Usage:
@printing_name_beforehand
def some_func(...):
# whatever
(Forgive me if there are typos; composing this reply on a tablet is
cumbersome.)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-25 18:00 -0800
Re: Finding the name of a function while defining it Roy Smith <roy@panix.com> - 2012-12-25 22:11 -0500
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:26 -0800
Re: Finding the name of a function while defining it Chris Angelico <rosuav@gmail.com> - 2012-12-27 18:48 +1100
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:52 -0800
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:52 -0800
Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 12:59 +0000
Re: Finding the name of a function while defining it Tim Roberts <timr@probo.com> - 2012-12-26 20:52 -0800
Re: Finding the name of a function while defining it Chris Angelico <rosuav@gmail.com> - 2012-12-27 16:44 +1100
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:46 -0800
Re: Finding the name of a function while defining it Chris Angelico <rosuav@gmail.com> - 2012-12-27 18:52 +1100
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:55 -0800
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:55 -0800
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:46 -0800
Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 10:58 +0000
Re: Finding the name of a function while defining it Tim Chase <python.list@tim.thechases.com> - 2012-12-27 07:32 -0600
Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 13:34 +0000
Re: Finding the name of a function while defining it Roy Smith <roy@panix.com> - 2012-12-27 10:09 -0500
Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-28 01:07 +0000
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-26 23:45 -0800
Re: Finding the name of a function while defining it Mitya Sirenef <msirenef@lightbird.net> - 2012-12-27 03:03 -0500
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:26 -0800
Re: Finding the name of a function while defining it Mitya Sirenef <msirenef@lightbird.net> - 2012-12-27 04:07 -0500
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:26 -0800
Re: Finding the name of a function while defining it Chris Rebert <clp2@rebertia.com> - 2012-12-27 00:58 -0800
Re: Finding the name of a function while defining it Tim Roberts <timr@probo.com> - 2012-12-28 21:01 -0800
Re: Finding the name of a function while defining it Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-12-29 12:18 +0200
Re: Finding the name of a function while defining it Chris Rebert <clp2@rebertia.com> - 2012-12-27 00:26 -0800
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:42 -0800
Re: Finding the name of a function while defining it Abhas Bhattacharya <abhasbhattacharya2@gmail.com> - 2012-12-27 00:42 -0800
Re: Finding the name of a function while defining it Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-27 13:31 +0000
Re: Finding the name of a function while defining it alex23 <wuwei23@gmail.com> - 2012-12-27 17:25 -0800
csiph-web