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: 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 Subject: Re: generate properties code in class dynamically Date: Thu, 12 May 2011 16:04:07 -0400 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 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 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