getting a class attribute using a keyword argument
wittempj at hotmail.com
wittempj at hotmail.com
Mon Jan 17 06:25:21 EST 2005
Guy Robinson wrote:
> Hello,
>
> I have a list of class instances. I wish to get the appropriate class
attribute
> in each class instance depending on a SINGLE keyword in the calling
class.
>
> How do I get the calling method to correctly recognise the keyword as
a keyword
> and not a class attribute? See example code below (which doesn't
work).
>
> class tocall:
> def __init__(self):
> self.title = "test"
> self.name = "name"
>
> def callingmethod(self,**kw):
> for key in kw:
> if tocall.key == kw[key]:
> return tocall.key
>
> which should work as such(but doesn't):
>
> print callmethod(title = "test")
> print callmethod(name = "name")
>
> Regards,
>
> Guy
You probably want something like this:
class tocall:
def __init__(self):
self.title = "test"
self.name = "name"
x = tocall()
print getattr(x, 'title')
print getattr(x, 'name')
print getattr(x, 'bogus')
More information about the Python-list
mailing list