Mailing List Archive

Reflection in Python?
I've just started programming in Python, and I
could really use some help figuring out something.
I'd like to implement Kent Beck's unit tester in
Python (someone posted a very nice alternative a
few weeks ago, but Junit's approach works better
for my project's needs). How do I do the
equivalent of what Java calls reflection? I want
to do the following:

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.
Reflection in Python? [ In reply to ]
From: "Darrell" <news@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@my-deja.com> wrote in message news:7ldglr$ita$1@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.
Reflection in Python? [ In reply to ]
From: anders777@my-deja.com

I've just started programming in Python, and I
could really use some help figuring out something.
I'd like to implement Kent Beck's unit tester in
Python (someone posted a very nice alternative a
few weeks ago, but Junit's approach works better
for my project's needs). How do I do the
equivalent of what Java calls reflection? I want
to do the following:

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.
Reflection in Python? [ In reply to ]
# 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@my-deja.com> wrote in message news:7ldglr$ita$1@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.
Reflection in Python? [ In reply to ]
anders777@my-deja.com writes:

> I've just started programming in Python, and I
> could really use some help figuring out something.
> I'd like to implement Kent Beck's unit tester in
> Python (someone posted a very nice alternative a
> few weeks ago, but Junit's approach works better
> for my project's needs). How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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
>
Not entirely sure I follow, but will this do what you want?

class Test:
def __init__(self,object,meth):
self.object = object
self.meth = meth
def run():
try:
getattr(self.object,self.meth)()
except:
print self.object.__class__.__name__, "failed!"

then

test = Test(MathTest,"testAdd")
test.run()

does what you ask (I think).

Anyway, I suspect the builtin "getattr" is what you're after.

HTH
Michael
Reflection in Python? [ In reply to ]
In article <7ldglr$ita$1@nnrp1.deja.com>,
anders777@my-deja.com wrote:
> ...How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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?
>
If I understand what you are trying to do, it's
easy in Python. Assuming MathTest is an
object instance:
test = MathTest.testAdd
will return you a bound method object which you
can then call as:
test()
Getting the name of the object class and method
is also easy. There are several ways to do this:
1) Define a doc string within the method by
putting a quoted string as the first line
under the def line. You can then refer to it
as "test.__doc__". This could say, for
example, "Method bar of class foo".
2) "test.__name__" will return the name of the
method. "test.im_class.__name__" will
return the name of the class that the method
is defined within.

Example:
class MathTest:
def testAdd(self, parm1, parm2):
"Method testAdd of Class MathTest"
result = parm1 + parm2
return result
<<< mt = MathTest()
<<< test = mt.testAdd
<<< test.run(2,2)
4
<<< print test.__doc__
Method testAdd of Class MathTest
<<< print test.__name__
testAdd
<<< print test.im_class.__name__
MathTest


I hope that helps.

- Roger




> 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.
>



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Reflection in Python? [ In reply to ]
From: rdev@my-deja.com

In article <7ldglr$ita$1@nnrp1.deja.com>,
anders777@my-deja.com wrote:
> ...How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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?
>
If I understand what you are trying to do, it's
easy in Python. Assuming MathTest is an
object instance:
test = MathTest.testAdd
will return you a bound method object which you
can then call as:
test()
Getting the name of the object class and method
is also easy. There are several ways to do this:
1) Define a doc string within the method by
putting a quoted string as the first line
under the def line. You can then refer to it
as "test.__doc__". This could say, for
example, "Method bar of class foo".
2) "test.__name__" will return the name of the
method. "test.im_class.__name__" will
return the name of the class that the method
is defined within.

Example:
class MathTest:
def testAdd(self, parm1, parm2):
"Method testAdd of Class MathTest"
result = parm1 + parm2
return result
<<< mt = MathTest()
<<< test = mt.testAdd
<<< test.run(2,2)
4
<<< print test.__doc__
Method testAdd of Class MathTest
<<< print test.__name__
testAdd
<<< print test.im_class.__name__
MathTest


I hope that helps.

- Roger




> 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.
>



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Reflection in Python? [ In reply to ]
In article <7ldglr$ita$1@nnrp1.deja.com>,
anders777@my-deja.com wrote:
> ...How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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?
>
If I understand what you are trying to do, it's
easy in Python. Assuming MathTest is an
object instance:
test = MathTest.testAdd
will return you a bound method object which you
can then call as:
test()
Getting the name of the object class and method
is also easy. There are several ways to do this:
1) Define a doc string within the method by
putting a quoted string as the first line
under the def line. You can then refer to it
as "test.__doc__". This could say, for
example, "Method bar of class foo".
2) "test.__name__" will return the name of the
method. "test.im_class.__name__" will
return the name of the class that the method
is defined within.

Example:
class MathTest:
def testAdd(self, parm1, parm2):
"Method testAdd of Class MathTest"
result = parm1 + parm2
return result
<<< mt = MathTest()
<<< test = mt.testAdd
<<< test.run(2,2)
4
<<< print test.__doc__
Method testAdd of Class MathTest
<<< print test.__name__
testAdd
<<< print test.im_class.__name__
MathTest


I hope that helps.

- Roger




> 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.
>



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Reflection in Python? [ In reply to ]
From: rdev@my-deja.com

In article <7ldglr$ita$1@nnrp1.deja.com>,
anders777@my-deja.com wrote:
> ...How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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?
>
If I understand what you are trying to do, it's
easy in Python. Assuming MathTest is an
object instance:
test = MathTest.testAdd
will return you a bound method object which you
can then call as:
test()
Getting the name of the object class and method
is also easy. There are several ways to do this:
1) Define a doc string within the method by
putting a quoted string as the first line
under the def line. You can then refer to it
as "test.__doc__". This could say, for
example, "Method bar of class foo".
2) "test.__name__" will return the name of the
method. "test.im_class.__name__" will
return the name of the class that the method
is defined within.

Example:
class MathTest:
def testAdd(self, parm1, parm2):
"Method testAdd of Class MathTest"
result = parm1 + parm2
return result
<<< mt = MathTest()
<<< test = mt.testAdd
<<< test.run(2,2)
4
<<< print test.__doc__
Method testAdd of Class MathTest
<<< print test.__name__
testAdd
<<< print test.im_class.__name__
MathTest


I hope that helps.

- Roger




> 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.
>



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Reflection in Python? [ In reply to ]
In article <7ldglr$ita$1@nnrp1.deja.com>,
anders777@my-deja.com wrote:
> ...How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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?
>
If I understand what you are trying to do, it's
easy in Python. Assuming MathTest is an
object instance:
test = MathTest.testAdd
will return you a bound method object which you
can then call as:
test()
Getting the name of the object class and method
is also easy. There are several ways to do this:
1) Define a doc string within the method by
putting a quoted string as the first line
under the def line. You can then refer to it
as "test.__doc__". This could say, for
example, "Method bar of class foo".
2) "test.__name__" will return the name of the
method. "test.im_class.__name__" will
return the name of the class that the method
is defined within.

Example:
class MathTest:
def testAdd(self, parm1, parm2):
"Method testAdd of Class MathTest"
result = parm1 + parm2
return result
<<< mt = MathTest()
<<< test = mt.testAdd
<<< test.run(2,2)
4
<<< print test.__doc__
Method testAdd of Class MathTest
<<< print test.__name__
testAdd
<<< print test.im_class.__name__
MathTest


I hope that helps.

- Roger




Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Reflection in Python? [ In reply to ]
From: rdev@my-deja.com

In article <7ldglr$ita$1@nnrp1.deja.com>,
anders777@my-deja.com wrote:
> ...How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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?
>
If I understand what you are trying to do, it's
easy in Python. Assuming MathTest is an
object instance:
test = MathTest.testAdd
will return you a bound method object which you
can then call as:
test()
Getting the name of the object class and method
is also easy. There are several ways to do this:
1) Define a doc string within the method by
putting a quoted string as the first line
under the def line. You can then refer to it
as "test.__doc__". This could say, for
example, "Method bar of class foo".
2) "test.__name__" will return the name of the
method. "test.im_class.__name__" will
return the name of the class that the method
is defined within.

Example:
class MathTest:
def testAdd(self, parm1, parm2):
"Method testAdd of Class MathTest"
result = parm1 + parm2
return result
<<< mt = MathTest()
<<< test = mt.testAdd
<<< test.run(2,2)
4
<<< print test.__doc__
Method testAdd of Class MathTest
<<< print test.__name__
testAdd
<<< print test.im_class.__name__
MathTest


I hope that helps.

- Roger




Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Reflection in Python? [ In reply to ]
Assuming you're working with Python class instance objects,
something like the following might work:

# define class
class SillyMathTest:
def testAdd(self, x, y):
return x+y

# create class instance object
MathTest = SillyMathTest()

# get reference to bound method MathTest.testAdd
test = getattr(MathTest, "testAdd")

# effectively do MathTest.testAdd(3,5)
test(3,5)

As to getting naming info: well, you already have the
name of the method as a string, you can get the name of
the class as MathTest.__class__.__name__, but there
is no way to extract the string "MathTest" from the
MathTest object because "MathTest" is the name of
a variable referencing the object, not something that
is really part of the object. (You couldn't do this in
Java either, so I'm not sure what you had in mind.)

Bob Wentworth



--original message--
I've just started programming in Python, and I
could really use some help figuring out something.
I'd like to implement Kent Beck's unit tester in
Python (someone posted a very nice alternative a
few weeks ago, but Junit's approach works better
for my project's needs). How do I do the
equivalent of what Java calls reflection? I wantto do the
following:

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. Anythoughts?
Reflection in Python? [ In reply to ]
From: Michael Hudson <mwh21@cam.ac.uk>

anders777@my-deja.com writes:

> I've just started programming in Python, and I
> could really use some help figuring out something.
> I'd like to implement Kent Beck's unit tester in
> Python (someone posted a very nice alternative a
> few weeks ago, but Junit's approach works better
> for my project's needs). How do I do the
> equivalent of what Java calls reflection? I want
> to do the following:
>
> 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
>
Not entirely sure I follow, but will this do what you want?

class Test:
def __init__(self,object,meth):
self.object = object
self.meth = meth
def run():
try:
getattr(self.object,self.meth)()
except:
print self.object.__class__.__name__, "failed!"

then

test = Test(MathTest,"testAdd")
test.run()

does what you ask (I think).

Anyway, I suspect the builtin "getattr" is what you're after.

HTH
Michael
Reflection in Python? [ In reply to ]
> I've just started programming in Python, and I could really use some
> help figuring out something. I'd like to implement Kent Beck's unit
> tester in Python (someone posted a very nice alternative a few weeks
> ago, but Junit's approach works better for my project's needs). How
> do I do the equivalent of what Java calls reflection?

I am not familiar with Kent Beck's unit tester, but I did write a note
a while ago about Python and reflection. This work has now matured a
little bit more, and is a part of my Dr. thesis that will be submitted
soon. Anyway, the note is available at:

http://www.itek.norut.no/~anders/abstract/andersen98a.html

> Thanks, Anders Schneiderman

Anders,

--
Anders Andersen - <mailto:aa@computer.org> - <http://w3.to/andersen/>
Reflection in Python? [ In reply to ]
If you are into reflection, you should really take a look at CLOS,
before setting things in stone in Python.

You can find my stuff in
http://ligwww.epfl.ch/matomira/en/publications.html (the last book was
finally released in '96 as "Advances in Metalevel Architectures and
Reflection"). For DB interface stuff, there was
paper in LUV'95 I think.
Reflection in Python? [ In reply to ]
Why do you post it 3 times? Is it such important?

--
Felix Puetsch
Reflection in Python? [ In reply to ]
From: puetsch@gmx.de (Felix Puetsch)

Why do you post it 3 times? Is it such important?

--
Felix Puetsch
Reflection in Python? [ In reply to ]
From: Anders Andersen <AAndersen@ACM.Org>

> I've just started programming in Python, and I could really use some
> help figuring out something. I'd like to implement Kent Beck's unit
> tester in Python (someone posted a very nice alternative a few weeks
> ago, but Junit's approach works better for my project's needs). How
> do I do the equivalent of what Java calls reflection?

I am not familiar with Kent Beck's unit tester, but I did write a note
a while ago about Python and reflection. This work has now matured a
little bit more, and is a part of my Dr. thesis that will be submitted
soon. Anyway, the note is available at:

http://www.itek.norut.no/~anders/abstract/andersen98a.html

> Thanks, Anders Schneiderman

Anders,

--
Anders Andersen - <mailto:aa@computer.org> - <http://w3.to/andersen/>
Reflection in Python? [ In reply to ]
From: Fernando Mato Mira <matomira@iname.com>

If you are into reflection, you should really take a look at CLOS,
before setting things in stone in Python.

You can find my stuff in
http://ligwww.epfl.ch/matomira/en/publications.html (the last book was
finally released in '96 as "Advances in Metalevel Architectures and
Reflection"). For DB interface stuff, there was
paper in LUV'95 I think.