Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feed.xsnews.nl!border-3.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; 'subject:Python': 0.05; 'attribute': 0.05; 'attributes': 0.07; 'executed': 0.07; 'python': 0.09; '###': 0.09; 'ast': 0.09; 'name):': 0.09; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:members': 0.09; 'terry': 0.09; 'def': 0.10; 'cases': 0.15; 'bytecode': 0.16; 'compilation,': 0.16; 'jure': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:class': 0.16; 'x.y': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'variable': 0.20; 'define': 0.20; 'trying': 0.21; 'assuming': 0.22; 'class.': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'header:X-Complaints- To:1': 0.28; 'searches': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'becomes': 0.30; 'code': 0.31; 'asking': 0.32; 'int': 0.33; 'purposes,': 0.33; 'to:addr:python-list': 0.33; 'done': 0.34; 'whatever': 0.35; 'nature': 0.35; 'something': 0.35; 'received:org': 0.36; 'subject:with': 0.36; 'subject: (': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'object': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'most': 0.61; 'first': 0.61; 'union': 0.66; 'received:fios.verizon.net': 0.84; 'returns.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Fool Python class with imaginary members (serious guru stuff inside) Date: Thu, 20 Sep 2012 13:04:31 -0400 References: <89b80b56-d1f2-477e-b28a-4410f2cf9de6@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: <89b80b56-d1f2-477e-b28a-4410f2cf9de6@googlegroups.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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348160696 news.xs4all.nl 6967 [2001:888:2000:d::a6]:41296 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29561 On 9/20/2012 9:52 AM, Jure Erzno=C5=BEnik wrote: > I'm trying to create a class that would lie to the user that a member i= s in some cases a simple variable and in other cases a class. The nature = of the member would depend on call syntax like so: > 1. x =3D obj.member #x becomes the "simple" value contained in member > 2. x =3D obj.member.another_member #x becomes the "simple" value contai= ned in first member's another_member. x.y.z is parsed and executed as (x.y).z, so you are asking if the=20 attribute-getter can know what will be done with the object it returns. Assuming CPython, you would have to write something that searches the=20 Python code before compilation, the ast during compilation, or the=20 bytecode after compilation. Much easier would be to define a union class that is a simple type with=20 attributes and return that in the first lookup. class AttrInt(int): def __getattr__(self, name): return 'attribute' y =3D AttrInt(3) print(y, y.a) ### 3 attribute If x.y returns an AttrInt, it will act like an int for most purposes,=20 while x.y.z will return whatever AttrInt.__getattr__ does and the=20 temporary AttrInt y disappears. --=20 Terry Jan Reedy