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 wrote: > On Fri, Nov 18, 2011 at 7:51 AM, 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 thi

Re: Dynamically Generate Methods

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

Re: Dynamically Generate Methods

2011-11-18 Thread Duncan Booth
GZ 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 other > way t

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) Ho

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, since