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


Groups > comp.lang.python > #15912

Re: Dynamically Generate Methods

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'operator': 0.04; 'attributes': 0.05; 'attribute': 0.07; 'correspond': 0.07; 'dict': 0.09; 'dynamically': 0.09; 'am,': 0.12; 'def': 0.14; 'cc:addr :python-list': 0.15; 'subject:Generate': 0.16; 'cheers,': 0.18; 'wrote:': 0.18; 'instance': 0.18; 'key.': 0.18; 'specifies': 0.18; 'cc:no real name:2**0': 0.21; 'expanded': 0.23; 'header:In-Reply- To:1': 0.23; 'variable': 0.24; 'cc:2**0': 0.25; 'function': 0.27; 'import': 0.28; 'cc:addr:python.org': 0.29; 'message- id:@mail.gmail.com': 0.29; 'class': 0.29; 'received:209.85.214': 0.30; 'objects.': 0.30; 'received:mail-bw0-f46.google.com': 0.30; 'nov': 0.31; 'hi,': 0.31; 'determined': 0.32; 'wondering': 0.32; 'list': 0.32; 'there': 0.33; 'setting': 0.34; 'only)': 0.34; 'however,': 0.34; 'record': 0.35; 'regular': 0.35; 'beginning': 0.36; 'fri,': 0.36; 'instead.': 0.36; 'but': 0.37; 'received:google.com': 0.38; 'primary': 0.38; 'received:209.85': 0.38; 'called': 0.38; 'subject:: ': 0.39; 'might': 0.39; 'received:209': 0.40; 'store': 0.40; 'your': 0.61; '2011': 0.62; 'factory': 0.73; 'subject:Methods': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=MWoPzfIdKBkAW6n7MEJXT7+yyX1lCCowTlTFjej1TxU=; b=JG44H/gl/fSi1sEa1JxdObZWz4F/DZ5JR0HB7cwc9aeWJ69kFU4sumd7svXJecJfTE 0eZNk+UNkVcrj+Hc/wt5gOhbtU79KihXCYaYUkAp63oK7h4zB/oPw5gSfNFL404oVjfL yi4kp2H85BHG1bKwCjIwFMjqtwqFczUtcK2ck=
MIME-Version 1.0
In-Reply-To <ad844f2a-8521-4ce4-8dfc-fb7e0fc505ad@q39g2000prg.googlegroups.com>
References <ad844f2a-8521-4ce4-8dfc-fb7e0fc505ad@q39g2000prg.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 18 Nov 2011 11:04:19 -0700
Subject Re: Dynamically Generate Methods
To GZ <zyzhu2000@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
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 <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.2838.1321639492.27778.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1321639492 news.xs4all.nl 6953 [2001:888:2000:d::a6]:54624
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:15912

Show key headers only | View raw


On Fri, Nov 18, 2011 at 7:51 AM, GZ <zyzhu2000@gmail.com> 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.

(Accidentally sent this to the OP only)

This is exactly what the attrgetter factory function produces.

from operator import attrgetter
get_key = attrgetter(*key_attrs)

But if your attribute names are variable and arbitrary, I strongly
recommend you store them in a dict instead.  Setting them as instance
attributes risks that they might conflict with the regular attributes
and methods on your objects.

Cheers,
Ian

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


Thread

Dynamically Generate Methods GZ <zyzhu2000@gmail.com> - 2011-11-18 06:51 -0800
  Re: Dynamically Generate Methods Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-11-18 17:56 +0100
  Re: Dynamically Generate Methods Duncan Booth <duncan.booth@invalid.invalid> - 2011-11-18 17:52 +0000
  Re: Dynamically Generate Methods Ian Kelly <ian.g.kelly@gmail.com> - 2011-11-18 11:04 -0700
    Re: Dynamically Generate Methods GZ <zyzhu2000@gmail.com> - 2011-11-20 03:23 -0800

csiph-web