Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attributes': 0.05; 'correspond': 0.07; 'exec': 0.07; 'python': 0.08; 'dynamically': 0.09; 'none)': 0.09; 'def': 0.14; 'cc:addr:python-list': 0.15; 'eval': 0.16; 'key)': 0.16; 'received:192.168.200': 0.16; 'subject:Generate': 0.16; 'thanks,': 0.18; 'wrote:': 0.18; 'exists': 0.18; 'key.': 0.18; 'specifies': 0.18; 'cc:no real name:2**0': 0.21; 'this?': 0.21; 'expanded': 0.23; 'header:In- Reply-To:1': 0.23; 'cc:2**0': 0.25; 'function': 0.27; 'skip:( 40': 0.28; 'cc:addr:python.org': 0.29; 'class': 0.29; 'module': 0.30; 'key,': 0.30; 'objects.': 0.30; "skip:' 10": 0.30; 'equivalent': 0.30; 'hi,': 0.31; 'determined': 0.32; 'wondering': 0.32; 'list': 0.32; 'this.': 0.33; 'there': 0.33; 'header:User-Agent:1': 0.33; 'realize': 0.34; 'skip:k 20': 0.34; 'however,': 0.34; 'record': 0.35; 'something': 0.36; 'beginning': 0.36; 'example,': 0.36; 'but': 0.37; 'primary': 0.38; 'called': 0.38; 'received:192': 0.38; "there's": 0.39; 'subject:: ': 0.39; 'skip:_ 10': 0.40; 'header:Received:6': 0.60; 'your': 0.61; 'high': 0.66; 'subject:Methods': 0.84 X-IronPort-AV: E=Sophos;i="4.69,533,1315173600"; d="scan'208";a="2408842" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Fri, 18 Nov 2011 17:56:29 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: GZ Subject: Re: Dynamically Generate Methods References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321635635 news.xs4all.nl 6871 [2001:888:2000:d::a6]:59218 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15904 GZ wrote: > Hi, > > I have a class Record and a list key_attrs that specifies the names of > all attributes that correspond to a primary key. > > I can write a function like this to get the primary key: > > def get_key(instance_of_record): > return tuple(instance_of_record.__dict__[k] for k in key_attrs) > > However, since key_attrs are determined at the beginning of the > program while get_key() will be called over and over again, I am > wondering if there is a way to dynamically generate a get_ley method > with the key attributes expanded to avoid the list comprehension/ > generator. > > For example, if key_attrs=['A','B'], I want the generated function to > be equivalent to the following: > > def get_key(instance_of_record): > return (instance_of_record['A'],instance_of_record['B'] ) > > I realize I can use eval or exec to do this. But is there any other > way to do this? > > Thanks, > gz > > > > Hi, you may want to do something like class Record(object): PRIMARY_KEY = [] def __init__(self): for key in self.PRIMARY_KEY: setattr(self, key, None) def getPrimaryKeyValues(self): return [ getattr(self, key) for key in self.PRIMARY_KEY] class FruitRecord(Record): PRIMARY_KEY = ['fruit_id', 'fruit_name'] JM PS : there's a high chance that a python module already exists to access your database with python objects.