Path: csiph.com!news.mixmin.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!fu-berlin.de!uni-berlin.de!not-for-mail From: "Joseph L. Casale" Newsgroups: comp.lang.python Subject: Re: Static caching property Date: Mon, 21 Mar 2016 16:49:47 +0000 Lines: 46 Message-ID: References: <35a5c4206a0c40e584d62d5d37b068b3@activenetwerx.com> , <56f0230e$0$1616$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de tCJbJiDdowwJRXBP2Uz3YAg0ZXEyQRALXH0WMMyGLu0Q== 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; 'static': 0.03; 'api': 0.09; '@property': 0.09; 'descriptor': 0.09; 'instance.': 0.09; 'specific.': 0.09; 'def': 0.13; 'java,': 0.15; '999': 0.16; 'attribute:': 0.16; 'deferred': 0.16; 'received:172.18.0': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'test()': 0.16; 'yup,': 0.16; 'refers': 0.18; "shouldn't": 0.18; '(in': 0.18; 'to:name:python-list@python.org': 0.20; 'java': 0.22; 'header:In- Reply-To:1': 0.24; 'sense': 0.26; 'var': 0.27; 'callable': 0.29; 'itself,': 0.29; 'classes': 0.30; 'non': 0.32; 'expensive': 0.32; 'class': 0.33; 'instances': 0.33; 'gets': 0.35; 'clear': 0.35; 'instance': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'thanks': 0.37; 'associated': 0.38; 'end': 0.39; 'test': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'execution,': 0.91 X-Authority-Analysis: v=2.1 cv=FIq7v7cs c=1 sm=1 tr=0 a=g3mLq75WYuDrh3Lt0JSDww==:117 a=g3mLq75WYuDrh3Lt0JSDww==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=P90J6pEA2ccA:10 a=8nJEP1OIZ-IA:10 a=7OsogOcEt9IA:10 a=MWWXQqbeSIeVeF8QtU0A:9 a=wPNLvfGTeEIA:10 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.activenetwerx.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD autolearn=ham autolearn_force=no version=3.4.0 Thread-Topic: Static caching property Thread-Index: AQHRg4dpC97vHCaJwEm/OBao3y7fUJ9kGfXygAAA4Ok= In-Reply-To: <56f0230e$0$1616$c3e8da3$5496439d@news.astraweb.com> Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.18.0.4] X-CMAE-Envelope: MS4wfOakQBgc3O6lxT4Izpc6ibf9JEMyzrrt8qE7Yl3FEeVUUkYuQtMxPiAFl2j2fk8UjHpKj2kzPmjX6L1Y9T9KRMKwV6/Rn03p078dlbGttdVBh0m/K1JR cE/YAm768+FDN42V8svVKtBXBXPWP8d2PFBiWv7CgzzBuSV5xyl/J67CkOYdmX3pqAX9AWbn0EjODA== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105373 > I think Joseph is using "static" in the Java sense of being associated wi= th > the class rather than an instance. (In Java, members of classes must be > known at compile-time.) Yup, so a single value on the class itself, not instance specific. > But what you can do is have the property refer to a class attribute: >=20 >=20 > py> class Test(object): > ... _private =3D 999 > ... @property > ... def x(self): > ... return type(self)._private > ... @x.setter > ... def x(self, value): > ... type(self)._private =3D value > ... > py> a =3D Test() > py> b =3D Test() > py> c =3D Test() > py> a.x > 999 > py> b.x =3D 50 > py> c.x > 50 > py> a.x > 50 Right, but _private refers to an api call that is expensive and may not eve= n be accessed, so while I may new up three instances of Test across a, b and c, if none of= those end up accessing var x, it shouldn't get fetched. Without some deferred execution,= if the value of _private is a callable whose return value is what I am interested in, it= gets invoked the moment the class is compiled. In the non static sense this is trivial to accomplish with the descriptor p= rotocol, I am just not clear for the static sense. Thanks Ian and Steven, jlc