Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21098

Re: exec

Path csiph.com!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'parameter': 0.05; 'exec': 0.07; 'callable': 0.09; 'magnitude': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'value:': 0.09; 'def': 0.13; 'overloaded': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'then:': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'from:addr:web.de': 0.23; 'parameters.': 0.23; 'classes': 0.26; 'tried': 0.27; 'class': 0.29; 'quite': 0.31; 'objects': 0.32; 'object': 0.33; 'header:X-Complaints-To:1': 0.34; 'to:addr:python-list': 0.35; 'received:org': 0.36; 'either': 0.37; 'skip:_ 10': 0.38; 'data': 0.38; 'correctly': 0.39; 'to:addr:python.org': 0.40; 'special': 0.66; 'iron': 0.67; 'self.value': 0.84; 'value):': 0.84; 'wester': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: exec
Date Thu, 01 Mar 2012 18:14:54 +0100
Organization None
References <4f4f7527$1@news.fhg.de> <4f4f9d55$1@news.fhg.de>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b474.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.328.1330622109.3037.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330622109 news.xs4all.nl 6962 [2001:888:2000:d::a6]:42953
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21098

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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