Reflection in Python?

Darrell Darrell at p98.f112.n480.z2.fidonet.org
Wed Jun 30 05:19:24 EDT 1999


From: "Darrell" <news at dorb.com>

    # Get the testAdd method or attribute of MathTest
test=getattr(MathTest,'testAdd')

>>> class A:
...     def myX(self):
...             print 'xxx'
...
>>> a=A()
>>> af=getattr(a,'myX')
>>> apply(af)
xxx
>>> af()
xxx
        # Get the name of 'a'
>>> print a
<__main__.A instance at 7f79d0>
>>> print `a`
<__main__.A instance at 7f79d0>
>>> print str(a)
<__main__.A instance at 7f79d0>
>>> print af
<method A.myX of A instance at 7f79d0>
>>>
--
--Darrell
<anders777 at my-deja.com> wrote in message news:7ldglr$ita$1 at nnrp1.deja.com...
> test = MathTest("testAdd")
>
> where test.run() will run the "testAdd" method on
> the MathTest object?  I also want to be able to
> tell the user the name of the object (MathTest)
> and the method in case the method fails.  Any
> thoughts?
>
> Thanks,
> Anders Schneiderman
> National Journal Daily Briefings Group
>
>
>
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.





More information about the Python-list mailing list