how to pass attribute name via sys.argv

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Felix Hebeler

    #1

    how to pass attribute name via sys.argv

    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
    [color=blue][color=green][color=darkred]
    >>> attrName = sys.argv[1]
    >>> attrName[/color][/color][/color]
    '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
  • Wolfram Kraus

    #2
    Re: how to pass attribute name via sys.argv

    Felix Hebeler wrote:[color=blue]
    > 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
    >[color=green][color=darkred]
    >>>> attrName = sys.argv[1] attrName[/color][/color]
    > 'cellsize'
    >
    > and I need to pass it on so I can call
    >
    > value = object.cellsize[0][/color]
    Use getattr:
    value = getattr(object, attrName)[0]
    [color=blue]
    >
    > Can this be done using Python?
    >
    > Thanks for any hints
    >
    > Cheers Felix[/color]

    HTH,
    Wolfram

    Comment

    • Gilles Lenfant

      #3
      Re: how to pass attribute name via sys.argv

      Felix Hebeler a écrit :[color=blue]
      > 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
      >[color=green][color=darkred]
      > >>> attrName = sys.argv[1]
      > >>> attrName[/color][/color]
      > '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[/color]

      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

      Comment

      • Felix Hebeler

        #4
        Re: how to pass attribute name via sys.argv

        Wolfram Kraus wrote:[color=blue]
        > Felix Hebeler wrote:
        >[/color]
        <snip>[color=blue][color=green]
        >>
        >> I need to call an object attribute:
        >>
        >> value = object.attrName[0]
        >>[/color][/color]
        <snip>[color=blue]
        >
        > Use getattr:
        > value = getattr(object, attrName)[0]
        >[/color]
        [color=blue]
        >
        >
        > HTH,
        > Wolfram[/color]

        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

        Comment

        Working...