accessing object attribute as parameter

Discussion in 'Python' started by ajikoe@gmail.com, Jun 26, 2005.

  1. Guest

    Hello,

    I would like to access object parameter / variable from a function.

    For example :
    class A:
    def __init__(self, x,y):
    self.x = x
    self.y = y

    in the main program, I have a list of obj A:
    L = [A(1,2), A(2,3)]

    Now I need to access the value of x or y from a function:
    def GetSomething(seqObj, ?):
    return [obj.? from seqObj]


    How can I access x or y through this function ?

    Thanks,
    Pujo
     
    , Jun 26, 2005
    #1
    1. Advertising

  2. wrote:
    > Hello,
    >
    > I would like to access object parameter / variable from a function.
    >
    > For example :
    > class A:
    > def __init__(self, x,y):
    > self.x = x
    > self.y = y
    >
    > in the main program, I have a list of obj A:
    > L = [A(1,2), A(2,3)]
    >
    > Now I need to access the value of x or y from a function:
    > def GetSomething(seqObj, ?):
    > return [obj.? from seqObj]
    >
    >
    > How can I access x or y through this function ?


    Use getattr:

    def GetSomething(seqObj, name):
    return [getattr(obj, name) for obj in seqObj]


    Diez
     
    Diez B. Roggisch, Jun 26, 2005
    #2
    1. Advertising

  3. Guest

    thanks Diez

    pujo
     
    , Jun 26, 2005
    #3
    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. johnny
    Replies:
    5
    Views:
    757
    johnny
    Dec 10, 2006
  2. 陈伟
    Replies:
    9
    Views:
    279
    Hans Mulder
    Aug 30, 2012
  3. Marc Aymerich
    Replies:
    0
    Views:
    88
    Marc Aymerich
    Nov 24, 2013
  4. Devin Jeanpierre
    Replies:
    12
    Views:
    122
    Steven D'Aprano
    Nov 24, 2013
  5. Chris Angelico
    Replies:
    0
    Views:
    72
    Chris Angelico
    Nov 24, 2013
Loading...

Share This Page