getting a class attribute using a keyword argument

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guy Robinson

    #1

    getting a class attribute using a keyword argument

    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(s elf,**kw):
    for key in kw:
    if tocall.key == kw[key]:
    return tocall.key

    which should work as such(but doesn't):

    print callmethod(titl e = "test")
    print callmethod(name = "name")

    Regards,

    Guy
  • Nick Coghlan

    #2
    Re: getting a class attribute using a keyword argument

    Guy Robinson wrote:[color=blue]
    > 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.[/color]

    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 | ncoghlan@email. com | Brisbane, Australia
    ---------------------------------------------------------------

    Comment

    • wittempj@hotmail.com

      #3
      Re: getting a class attribute using a keyword argument


      Guy Robinson wrote:[color=blue]
      > Hello,
      >
      > I have a list of class instances. I wish to get the appropriate class[/color]
      attribute[color=blue]
      > in each class instance depending on a SINGLE keyword in the calling[/color]
      class.[color=blue]
      >
      > How do I get the calling method to correctly recognise the keyword as[/color]
      a keyword[color=blue]
      > and not a class attribute? See example code below (which doesn't[/color]
      work).[color=blue]
      >
      > class tocall:
      > def __init__(self):
      > self.title = "test"
      > self.name = "name"
      >
      > def callingmethod(s elf,**kw):
      > for key in kw:
      > if tocall.key == kw[key]:
      > return tocall.key
      >
      > which should work as such(but doesn't):
      >
      > print callmethod(titl e = "test")
      > print callmethod(name = "name")
      >
      > Regards,
      >
      > Guy[/color]
      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')

      Comment

      • John Hsu

        #4
        Re: getting a class attribute using a keyword argument

        Guy Robinson wrote:
        [color=blue]
        > 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(s elf,**kw):
        > for key in kw:
        > if tocall.key == kw[key]:
        > return tocall.key
        >
        > which should work as such(but doesn't):
        >
        > print callmethod(titl e = "test")
        > print callmethod(name = "name")
        >
        > Regards,
        >
        > Guy[/color]

        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(titl e = "test")
        print callmethod(name = "name")

        Comment

        Working...