how to access data attributes of a class object

Discussion in 'Python' started by namrata, Jun 4, 2010.

  1. namrata

    namrata Guest

    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.
     
    namrata, Jun 4, 2010
    #1
    1. Advertisements

  2. namrata

    Peter Otten Guest

    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
     
    Peter Otten, Jun 4, 2010
    #2
    1. Advertisements

  3. namrata

    namrata Guest

    Thanks a lot Peter. It works.

    On Jun 4, 12:26 pm, Peter Otten <> 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
     
    namrata, Jun 4, 2010
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    5,322
    Thomas Weidenfeller
    Oct 12, 2005
  2. Max
    Replies:
    1
    Views:
    577
    Joe Kesselman
    Sep 22, 2006
  3. P4trykx
    Replies:
    2
    Views:
    2,011
    bruce barker
    Jan 31, 2007
  4. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    521
    Bruno Desthuilliers
    Jun 20, 2007
  5. Jayden
    Replies:
    16
    Views:
    704
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page