accessing object attribute as parameter

A

ajikoe

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
 
D

Diez B. Roggisch

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top