Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; 'only,': 0.07; 'subject:How': 0.09; 'python': 0.09; 'defined.': 0.09; 'namespace': 0.09; 'subject:module': 0.09; 'subject:skip:a 10': 0.09; 'that).': 0.09; '{})': 0.09; 'def': 0.10; 'foo()': 0.16; 'foo(object):': 0.16; 'subject: \n ': 0.16; 'hack': 0.18; 'import': 0.21; 'meant': 0.21; 'file:': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'header:In-Reply- To:1': 0.25; 'looks': 0.26; 'cc:addr:gmail.com': 0.27; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'url:python': 0.32; '-----': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'there': 0.35; 'url:org': 0.36; 'modules': 0.36; 'subject:: ': 0.38; 'object': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'subject:-': 0.40; 'url:mail': 0.40; 'think': 0.40; 'subject:, ': 0.61; 'received:194': 0.61; '(global': 0.84; 'subject:\t': 0.91; 'subject:add': 0.91 X-IronPort-AV: E=Sophos;i="4.80,442,1344204000"; d="scan'208";a="732307" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Tue, 18 Sep 2012 11:26:24 +0200 (CEST) From: Jean-Michel Pichavant To: python-list@python.org In-Reply-To: <74247047-c5b5-42a0-af69-4c520a1d5d8d@googlegroups.com> Subject: Re: How to use __getattr__ to the current module, for automatically add attributes of the real-time MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669) Cc: sergeiiv65@gmail.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347960389 news.xs4all.nl 6847 [2001:888:2000:d::a6]:41647 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29419 ----- Original Message ----- > Want to work so: > > import sys > class Foo(object): > def __getattr__(self, t): > print 'use __getattr__ - ', t > return type(t, (object,), {}) > def funct1(self): pass > def funct2(self): pass > > sys.modules[__name__] = Foo() > ttt('yy') > > name 'ttt' is not defined. > __getattr__ not work (((. > -- > http://mail.python.org/mailman/listinfo/python-list > I think __getattr__ would be triggered by using Foo().ttt or getattr(Foo(), 'ttt'). I think this hack is meant to work on modules object only, not in the global namespace where locals() and globals() are used instead (global namespace may not be the proper word, I don't know if there is a technical term for that). Here's how to make it work: foo.py import sys class Foo(object): def __getattr__(self, t): print 'use __getattr__ - ', t return type(t, (object,), {}) def funct1(self): pass def funct2(self): pass sys.modules[__name__] = Foo() Now in a python shell, or in another file: import foo foo.ttt use __getattr__ - ttt use __getattr__ - ttt < JM PS : Looks like a hell of a hack