Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21099
| Date | Thu, 01 Mar 2012 18:23:58 +0100 |
|---|---|
| From | Rolf Wester <rolf.wester@ilt.fraunhofer.de> |
| User-Agent | Mozilla/5.0 (X11; Linux i686 on x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 |
| MIME-Version | 1.0 |
| Newsgroups | comp.lang.python |
| Subject | Re: exec |
| References | <4f4f7527$1@news.fhg.de> <4f4f9d55$1@news.fhg.de> <mailman.328.1330622109.3037.python-list@python.org> |
| In-Reply-To | <mailman.328.1330622109.3037.python-list@python.org> |
| X-Enigmail-Version | 1.3.5 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| Message-ID | <4f4fb151$1@news.fhg.de> (permalink) |
| Organization | Fraunhofer Gesellschaft (http://www.fraunhofer.de/) |
| Lines | 49 |
| Path | csiph.com!aioe.org!news.mixmin.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!usenet-feed.fhg.de!news.fhg.de!not-for-mail |
| Xref | csiph.com comp.lang.python:21099 |
Show key headers only | View raw
Thank you, that really made things much easier and admittedly much less nasty too. Regards Rolf On 01/03/12 18:14, Peter Otten wrote: > Rolf Wester wrote: > >> The reason to use exec is just laziness. I have quite a lot of classes >> representing material data and every class has a number of parameters. >> The parameter are Magnitude objects (they have a value and a unit and >> overloaded special functions to correctly handle the units). I want to >> have getter functions that either return the Magnitude object or just the >> value: >> >> iron = Iron() >> iron.rho(0) => value >> iron.rho() => Magnitude object >> >> def rho(self, uf=1): >> if uf == 1: >> return self._rho >> else: >> return self._rho.val >> >> And because this would mean quite a lot of writing I tried to do it with >> exec. > > Make the Magnitude class callable then: > >>>> class Magnitude(object): > ... def __init__(self, value): > ... self.value = value > ... def __call__(self, uf=1): > ... if uf == 1: > ... return self > ... return self.value > ... >>>> class Iron(object): > ... def __init__(self): > ... self.rho = Magnitude(42) > ... >>>> iron = Iron() >>>> iron.rho() > <__main__.Magnitude object at 0x7fb94062be10> >>>> iron.rho(0) > 42 > >
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
exec Rolf Wester <rolf.wester@ilt.fraunhofer.de> - 2012-03-01 14:07 +0100
Re: exec Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-03-01 14:46 +0100
Re: exec Arnaud Delobelle <arnodel@gmail.com> - 2012-03-01 14:13 +0000
Re: exec Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-01 14:21 +0000
Re: exec Rolf Wester <rolf.wester@ilt.fraunhofer.de> - 2012-03-01 16:58 +0100
Re: exec Michael Ströder <michael@stroeder.com> - 2012-03-01 17:26 +0100
Re: exec Peter Otten <__peter__@web.de> - 2012-03-01 18:14 +0100
Re: exec Rolf Wester <rolf.wester@ilt.fraunhofer.de> - 2012-03-01 18:23 +0100
Re: exec Peter Otten <__peter__@web.de> - 2012-03-02 08:26 +0100
csiph-web