Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to access data attributes of a class object

Reply
Thread Tools

how to access data attributes of a class object

 
 
namrata
Guest
Posts: n/a
 
      06-04-2010
Hi,

i am new to python and sqlalchemy. so my doubt mite be very silly.

I am creating an instance of a class. for example let the class be
Temp and the attributes of the class be first_name and last_name.
in order to access the attributes i will usually use the following
command:
a = Temp()
a.last_name = "*******"

now, i am using sqlalchemy and mapping a table in postgresql to
python. i am trying to implement a method where i can pass the column
name and a value to the method. the method will fire a query for that
column name and value. the query is as follows:
query = session.query(Temp).query.filter(Temp.last_name == some_value)

the column name passed to the method is stored in a local variable say
'col_name'
how do i pass this local variable to the query. when i try the below
query it throws an error saying col_name is not an attirbute
query = session.query(Temp).query.filter(Temp.col_name == some_value)

please help.
thanks in advance.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      06-04-2010
namrata wrote:

> I am creating an instance of a class. for example let the class be
> Temp and the attributes of the class be first_name and last_name.
> in order to access the attributes i will usually use the following
> command:
> a = Temp()
> a.last_name = "*******"
>
> now, i am using sqlalchemy and mapping a table in postgresql to
> python. i am trying to implement a method where i can pass the column
> name and a value to the method. the method will fire a query for that
> column name and value. the query is as follows:
> query = session.query(Temp).query.filter(Temp.last_name == some_value)
>
> the column name passed to the method is stored in a local variable say
> 'col_name'
> how do i pass this local variable to the query. when i try the below
> query it throws an error saying col_name is not an attirbute
> query = session.query(Temp).query.filter(Temp.col_name == some_value)


I'd guess you can use getattr():

query = session.query(Temp).query.filter(getattr(Temp, col_name) ==
some_value)

Peter
 
Reply With Quote
 
 
 
 
namrata
Guest
Posts: n/a
 
      06-04-2010
Thanks a lot Peter. It works.

On Jun 4, 12:26*pm, Peter Otten <(E-Mail Removed)> wrote:
> namrata wrote:
> > I am creating an instance of a class. for example let the class be
> > Temp and the attributes of the class be first_name and last_name.
> > in order to access the attributes i will usually use the following
> > command:
> > a = Temp()
> > a.last_name = "*******"

>
> > now, i am using sqlalchemy and mapping a table in postgresql to
> > python. i am trying to implement a method where i can pass the column
> > name and a value to the method. the method will fire a query for that
> > column name and value. the query is as follows:
> > query = session.query(Temp).query.filter(Temp.last_name == some_value)

>
> > the column name passed to the method is stored in a local variable say
> > 'col_name'
> > how do i pass this local variable to the query. when i try the below
> > query it throws an error saying col_name is not an attirbute
> > query = session.query(Temp).query.filter(Temp.col_name == some_value)

>
> I'd guess you can use getattr():
>
> query = session.query(Temp).query.filter(getattr(Temp, col_name) ==
> some_value)
>
> Peter


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
data attributes override method attributes? Jayden Python 16 09-29-2012 03:20 AM
While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments Krishna Python 4 03-07-2008 09:44 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments