getattr() question

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

    getattr() question

    Hi.

    I am trying to dynamically load a class and attributes at run time. I
    do not know what classes will be referenced until run time. I have it
    loading the module correctly, but when I use getattr to access the
    class and its attributes everything works except that I get additional
    unwanted output. The code



    testclass.py:

    #!/usr/bin/python

    class testclass(objec t):

    myname = ""

    def __init__(self, name):
    self.myname = name

    def view(self):
    print "hello %s" % self.myname



    test.py:

    #!/usr/bin/python

    import sys
    sys.path.append ('.')
    from pprint import pprint

    if __name__ == '__main__':
    myname = "testclass"
    myaction = "view"
    try:
    tc = __import__(myna me)
    myclass = getattr(tc,myna me)
    myinstance = getattr(myclass ('python n00b'), myaction,
    myaction)
    pprint(myinstan ce())
    except ImportError:
    "error"



    Here is the output that I get:

    user@debian:~/$ python test.py
    hello python n00b
    None
    user@debian:~/$


    Why is it printing 'None'? What am I doing wrong. I appreciate any
    help.
  • John Machin

    #2
    Re: getattr() question

    On Dec 23, 10:39 am, Sledge <andrew.j.sle.. .@gmail.comwrot e:
    Hi.
    >
    I am trying to dynamically load a class and attributes at run time. I
    do not know what classes will be referenced until run time. I have it
    loading the module correctly, but when I use getattr to access the
    class and its attributes everything works except that I get additional
    unwanted output. The code
    >
    testclass.py:
    >
    #!/usr/bin/python
    >
    class testclass(objec t):
    >
    myname = ""
    >
    def __init__(self, name):
    self.myname = name
    >
    def view(self):
    print "hello %s" % self.myname
    >
    test.py:
    >
    #!/usr/bin/python
    >
    import sys
    sys.path.append ('.')
    from pprint import pprint
    >
    if __name__ == '__main__':
    myname = "testclass"
    myaction = "view"
    try:
    tc = __import__(myna me)
    myclass = getattr(tc,myna me)
    myinstance = getattr(myclass ('python n00b'), myaction,
    myaction)
    pprint(myinstan ce())
    except ImportError:
    "error"
    What do you expect to see if the import fails?
    >
    Here is the output that I get:
    >
    user@debian:~/$ python test.py
    hello python n00b
    None
    user@debian:~/$
    >
    Why is it printing 'None'? What am I doing wrong. I appreciate any
    help.
    The problem is nothing to do with using getattr; it "works" in the
    sense that it does what you appear to want it to.

    You have *two* explict outputting statements: the print statement in
    the first file and the pprint invocation in the second file. Seems
    fairly obvious that it's not the first of these. So dissect
    "pprint(myinsta nce())".

    myinstance is bound to the view method [in the first file] which
    (implicitly) returns None. So you are in effect doing pprint(None).

    Aside: give your fingers a rest: don't type "my" so often.

    Comment

    • Sledge

      #3
      Re: getattr() question

      On Dec 22, 7:14 pm, John Machin <sjmac...@lexic on.netwrote:
      On Dec 23, 10:39 am, Sledge <andrew.j.sle.. .@gmail.comwrot e:
      >
      >
      >
      Hi.
      >
      I am trying to dynamically load a class and attributes at run time. I
      do not know what classes will be referenced until run time. I have it
      loading the module correctly, but when I use getattr to access the
      class and its attributes everything works except that I get additional
      unwanted output. The code
      >
      testclass.py:
      >
      #!/usr/bin/python
      >
      class testclass(objec t):
      >
      myname = ""
      >
      def __init__(self, name):
      self.myname = name
      >
      def view(self):
      print "hello %s" % self.myname
      >
      test.py:
      >
      #!/usr/bin/python
      >
      import sys
      sys.path.append ('.')
      from pprint import pprint
      >
      if __name__ == '__main__':
      myname = "testclass"
      myaction = "view"
      try:
      tc = __import__(myna me)
      myclass = getattr(tc,myna me)
      myinstance = getattr(myclass ('python n00b'), myaction,
      myaction)
      pprint(myinstan ce())
      except ImportError:
      "error"
      >
      What do you expect to see if the import fails?
      >
      >
      >
      Here is the output that I get:
      >
      user@debian:~/$ python test.py
      hello python n00b
      None
      user@debian:~/$
      >
      Why is it printing 'None'? What am I doing wrong. I appreciate any
      help.
      >
      The problem is nothing to do with using getattr; it "works" in the
      sense that it does what you appear to want it to.
      >
      You have *two* explict outputting statements: the print statement in
      the first file and the pprint invocation in the second file. Seems
      fairly obvious that it's not the first of these. So dissect
      "pprint(myinsta nce())".
      >
      myinstance is bound to the view method [in the first file] which
      (implicitly) returns None. So you are in effect doing pprint(None).
      that did the trick. How could I have missed something so obvious?
      >
      Aside: give your fingers a rest: don't type "my" so often.
      It was just for demonstration purposes :). Thanks for your help John.

      Comment

      Working...