[Tutor] Name of a module within itself
lonetwin@yahoo.com
steve@in.cqsl.com
Sat, 21 Jul 2001 16:02:08 +0530 (IST)
Hi there,
One quick question, suppose I create a file (say 'foo.py'), that has classes
MyFoo, HisFoo, HerFoo, and a function main(). Now, from main I need to
refer to the name of the module (foo), such that, I could do something like:
def main():
....
....
getattr([refer to this module], 'HisFoo')
....
....
how do I do that ?
from the prompt I can do:
============================================================
[steve@mercury ~]$ python
Python 2.1 (#3, Jun 25 2001, 13:39:27)
[GCC 2.95.3 19991030 (prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import foo
>>> getattr(foo, 'HisFoo')
<class foo.HisFoo at 0x810fde4>
>>> getattr(foo, 'HisFoo').x
'foo'
>>>
==============================================================
where the file 'foo.py' contains:
===================================
#!/usr/bin/python
class MyFoo:
pass
class HisFoo:
x = 'foo'
class HerFoo:
x = 'bar'
====================================
....ehe....Am I being clear enough ?...if not please tell me and I'll send
some more code along.
Peace
Steve
--
||||||||||||||||||||||
|||||||||#####||||||||
||||||||#######|||||||
||||||||# O O #|||||||
||||||||#\ ~ /#|||||||
||||||##||\_/||##|||||
|||||#||||||||||##||||
||||#||||||||||||##|||
||||#|||||||||||||##||
|||/\##|||||||||##/\||
|/ \#########/ \
|\ \#######/ /
||\____/#######\____/|
=====================================
Debug is human, de-fix divine.
====================================