Re: Dynamically Generate Methods

2011-11-20 Thread GZ
Hi All, I see. It works. Thanks, GZ On Nov 18, 12:04 pm, Ian Kelly ian.g.ke...@gmail.com wrote: On Fri, Nov 18, 2011 at 7:51 AM, GZ zyzhu2...@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

Dynamically Generate Methods

2011-11-18 Thread GZ
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,

Re: Dynamically Generate Methods

2011-11-18 Thread Jean-Michel Pichavant
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)

Re: Dynamically Generate Methods

2011-11-18 Thread Duncan Booth
GZ zyzhu2...@gmail.com wrote: 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

Re: Dynamically Generate Methods

2011-11-18 Thread Ian Kelly
On Fri, Nov 18, 2011 at 7:51 AM, GZ zyzhu2...@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