Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2b.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'else:': 0.03; 'method.': 0.05; 'none:': 0.05; 'sys': 0.05; 'method:': 0.09; 'obj': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:# 20': 0.13; 'def': 0.14; 'adam': 0.16; 'obj=none):': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sentinel': 0.16; 'wrote:': 0.16; 'attribute': 0.18; ';-)': 0.18; 'default,': 0.22; 'nested': 0.22; 'parameter': 0.22; 'ron': 0.22; 'am,': 0.23; '2015': 0.23; 'replacing': 0.23; 'cheers,': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'objects': 0.29; 'themselves': 0.29; "skip:' 10": 0.30; 'work.': 0.30; 'print': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'subject:?': 0.34; 'to:addr:python-list': 0.35; 'false': 0.35; 'modules': 0.36; "didn't": 0.37; 'subject:: ': 0.37; 'received:org': 0.38; 'means': 0.39; 'test': 0.39; 'to:addr:python.org': 0.39; 'subject:the': 0.40; 'yes': 0.60; 'your': 0.60; 'fun': 0.61; 'skip:n 10': 0.63; 'accessed': 0.66; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'reply-to:addr:gmail.com': 0.77; '__call__': 0.84; 'subject:Set': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ron Adam Subject: Re: Set a flag on the function or a global? Date: Tue, 16 Jun 2015 07:02:03 -0400 References: <557f6676$0$21718$c3e8da3@news.astraweb.com> <557fe949$0$1655$c3e8da3$5496439d@news.astraweb.com> Reply-To: ron3200@gmail.com Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ip98-170-222-146.pn.at.cox.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: <557fe949$0$1655$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 81 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434452540 news.xs4all.nl 2911 [2001:888:2000:d::a6]:47093 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92673 On 06/16/2015 05:15 AM, Steven D'Aprano wrote: > On Tuesday 16 June 2015 10:24, Ron Adam wrote: > >> >Another way is to make it an object with a __call__ method. >> > >> >The the attribute can be accessed from both outside and inside dependably. > That's what functions are, objects with a __call__ method: > > > py> (lambda: None).__call__ > Yes ;-) > One slight disadvantage is that functions don't take a "self" parameter by > default, which means they have to refer to themselves by name: > > def spam(): > print spam.attr > > > Here's a fun hack: > > py> from types import MethodType > py> def physician(func): > ... # As in, "Physician, Know Thyself":-) > ... return MethodType(func, func) > ... > py> @physician > ... def spam(this, n): > ... return this.food * n > ... > py> spam.__func__.food = "spam-and-eggs " > py> spam(3) > 'spam-and-eggs spam-and-eggs spam-and-eggs' How about this?: (paste it into your console) #--------------------- import sys class EDir: long = False def __call__(self, obj=None): if obj == None: d = sys._getframe(1).f_locals else: d = dir(obj) return [x for x in d if self.long or not x.startswith("_")] edir = EDir() edir() edir(edir) edir.long = True edir(edir) edir.long = False edir(edir) #---------------------- I didn't test how that works from other modules or in nested scopes. Also replacing None with a unique sentinel object may be better so dir(None) will work. Cheers, Ron