how to pass attribute name via sys.argv

Discussion in 'Python' started by Felix Hebeler, Jan 27, 2005.

  1. Hi all,
    I am doing some Python scripting for a while, but I'm not too deep into
    it yet. So I have a problem I can't solve.

    I need to call an object attribute:

    value = object.attrName[0]

    the problem is, that the attribute name can only be specified at runtime.

    So what I have is something like

    >>> attrName = sys.argv[1]
    >>> attrName

    'cellsize'

    and I need to pass it on so I can call

    value = object.cellsize[0]


    Can this be done using Python?

    Thanks for any hints

    Cheers
    Felix
     
    Felix Hebeler, Jan 27, 2005
    #1
    1. Advertising

  2. Felix Hebeler wrote:
    > Hi all, I am doing some Python scripting for a while, but I'm not too
    > deep into it yet. So I have a problem I can't solve.
    >
    > I need to call an object attribute:
    >
    > value = object.attrName[0]
    >
    > the problem is, that the attribute name can only be specified at
    > runtime.
    >
    > So what I have is something like
    >
    >>>> attrName = sys.argv[1] attrName

    > 'cellsize'
    >
    > and I need to pass it on so I can call
    >
    > value = object.cellsize[0]

    Use getattr:
    value = getattr(object, attrName)[0]

    >
    > Can this be done using Python?
    >
    > Thanks for any hints
    >
    > Cheers Felix


    HTH,
    Wolfram
     
    Wolfram Kraus, Jan 27, 2005
    #2
    1. Advertising

  3. Felix Hebeler a écrit :
    > Hi all,
    > I am doing some Python scripting for a while, but I'm not too deep into
    > it yet. So I have a problem I can't solve.
    >
    > I need to call an object attribute:
    >
    > value = object.attrName[0]
    >
    > the problem is, that the attribute name can only be specified at runtime.
    >
    > So what I have is something like
    >
    > >>> attrName = sys.argv[1]
    > >>> attrName

    > 'cellsize'
    >
    > and I need to pass it on so I can call
    >
    > value = object.cellsize[0]
    >
    >
    > Can this be done using Python?
    >
    > Thanks for any hints
    >
    > Cheers
    > Felix


    The builtin "setattr" is your friend.
    "object" is now a reserved (builtin) name, use "objekt" instead.

    class Foo(object):
    pass
    objekt = Foo()
    attrName = sys.argv[1]
    values = ['foo', 'bar', 'whatever']
    setattr(objekt, attrName, values)

    HTH

    --
    Gilles
     
    Gilles Lenfant, Jan 27, 2005
    #3
  4. Wolfram Kraus wrote:
    > Felix Hebeler wrote:
    >

    <snip>
    >>
    >> I need to call an object attribute:
    >>
    >> value = object.attrName[0]
    >>

    <snip>
    >
    > Use getattr:
    > value = getattr(object, attrName)[0]
    >


    >
    >
    > HTH,
    > Wolfram


    Thanks so much!
    Had I known earlier.
    Looks so easy...

    Now, why did I not find this in the online tutorial, the reference
    manual, or google?
    Not that I didn't try... I mean, I would find 'getattr' if I searched,
    but if you don't know what you're looking for..

    I find the reference manual extremely (== too) compact to look things up.
    A couple of colleages and me agreed that it is much more difficult to
    find solutions and _useful_ tips for Python than e.g. for Java (where
    there's Javadoc for example). The syntax doc in the reference manual to
    me looks like computer linguists might understand, but unfortunately not
    me. And Python code IS really easy to read, I agree, but what if I can't
    find out how to write it?
    I'd appreciate any link to online resources or recommendations for books
    (english/german)!

    Chances are I'm a silly/lazy/deprived/stupid bugger, but I try to think
    there's still hope!

    again, thank you so much for your quick response (thanks Gilles Lenfant
    too!), I really DO like the Python community ;-)

    Cheers
    Felix
     
    Felix Hebeler, Jan 27, 2005
    #4
    1. Advertising

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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,701
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    6,073
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,713
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    1,001
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    720
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page