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


Groups > comp.lang.python > #5254

Re: generate properties code in class dynamically

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'attributes': 0.05; 'versions,': 0.05; 'terry': 0.07; '(possibly': 0.09; 'dict': 0.09; 'dynamically': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; "'a'": 0.16; '(instance': 0.16; 'reedy': 0.16; 'sure,': 0.16; 'syntactic': 0.16; 'once,': 0.19; 'maybe': 0.21; 'jan': 0.22; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'keys': 0.23; 'subject:code': 0.23; 'version': 0.25; 'skip:[ 10': 0.26; 'classes': 0.26; 'class': 0.29; 'assuming': 0.29; 'decorators': 0.31; 'key,': 0.31; 'possible?': 0.31; 'to:addr :python-list': 0.32; 'done': 0.32; 'creates': 0.33; 'header:X -Complaints-To:1': 0.34; 'actually': 0.34; 'header:User-Agent:1': 0.35; 'body,': 0.35; 'like:': 0.35; 'properties': 0.36; 'exactly': 0.37; 'convenient': 0.38; 'but': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'could': 0.39; 'where': 0.39; 'header :Mime-Version:1': 0.39; 'add': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'property': 0.62; 'believe': 0.66; 'methods)': 0.84; 'subject:class': 0.84; 'subject:properties': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: generate properties code in class dynamically
Date Thu, 12 May 2011 16:04:07 -0400
References <f5a65253-def8-41c8-b722-97cd76bc72d0@32g2000vbe.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host rain.gmane.org
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10
In-Reply-To <f5a65253-def8-41c8-b722-97cd76bc72d0@32g2000vbe.googlegroups.com>
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.1489.1305230663.9059.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 82.94.164.166
X-Trace 1305230663 news.xs4all.nl 81475 [::ffff:82.94.164.166]:38335
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5254

Show key headers only | View raw


On 5/12/2011 9:11 AM, JamesEM wrote:

> I would prefer to generate the properties code dynamically from the
> keys of the dictionaries.
> What I am looking for is something like:
>
> class MyClass(object):
>
>      def __init__(self):
>          self.d = {}
>          d['field1'] = 1.0
>          d['field2'] = 'A'
>          d['field3'] = [10.0,20.0,30.0]
>          for f in d:
>             create_property(f)
>
> where create_property(f) dynamically creates the property code for
> field f in MyClass.
>
> Is this possible?

Without actually trying, I am not sure, but I believe maybe (possibly 
version dependent). The init method is the wrong place. Create the 
properties exactly once, just after the class is created. It is possible 
to add functions to classes as attributes (instance methods) after they 
are created. The property decorators *might* require that they be 
invoked with the class body, I do not know. I would first try with 
property().

Assuming dict name 'd' is fixed:

def gsd(key):
   def get(self):
     return self.d[key]
   def set(self, value):
     self.d[key] = value
   def del(self):
     del self.d[key]
   return get,set,del

for key in fieldnames:
   setattr(MyClass, key, property(*gsd(key)))

For recent versions, this could be done within a class decorator, but 
that is only convenient syntactic sugar.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

generate properties code in class dynamically JamesEM <james.housden@deutsche-boerse.com> - 2011-05-12 06:11 -0700
  Re: generate properties code in class dynamically nn <pruebauno@latinmail.com> - 2011-05-12 07:14 -0700
  Re: generate properties code in class dynamically Terry Reedy <tjreedy@udel.edu> - 2011-05-12 16:04 -0400
    Re: generate properties code in class dynamically JamesEM <james.housden@deutsche-boerse.com> - 2011-05-12 22:45 -0700

csiph-web