getting a class attribute using a keyword argument

Discussion in 'Python' started by Guy Robinson, Jan 17, 2005.

  1. Guy Robinson

    Guy Robinson Guest

    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
     
    Guy Robinson, Jan 17, 2005
    #1
    1. Advertising

  2. Guy Robinson

    Nick Coghlan Guest

    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.


    Py> help(getattr)
    Help on built-in function getattr in module __builtin__:

    getattr(...)
    getattr(object, name[, default]) -> value

    Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
    When a default argument is given, it is returned when the attribute doesn't
    exist; without it, an exception is raised in that case.

    Cheers,
    Nick.

    --
    Nick Coghlan | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
     
    Nick Coghlan, Jan 17, 2005
    #2
    1. Advertising

  3. Guy Robinson

    Guest

    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')
     
    , Jan 17, 2005
    #3
  4. Guy Robinson

    John Hsu Guest

    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


    Hi,

    This may be more like you want.

    class tocall:
    def __init__(self):
    self.title = "test"
    self.name = "name"

    def callmethod(**kw):
    for key in kw:
    if hasattr(tocall(), key):
    return getattr(tocall(), key)

    print callmethod(title = "test")
    print callmethod(name = "name")
     
    John Hsu, Jan 19, 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. Oktay Safak
    Replies:
    2
    Views:
    1,356
    Oktay Safak
    Aug 2, 2003
  2. Steven Bethard

    keyword argument for min/max

    Steven Bethard, Nov 30, 2004, in forum: Python
    Replies:
    1
    Views:
    321
    Steven Bethard
    Nov 30, 2004
  3. Replies:
    6
    Views:
    486
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    376
  5. xhm
    Replies:
    8
    Views:
    847
    James Kanze
    Jan 25, 2010
Loading...

Share This Page