[Tutor] Name of a module within itself
steve
lonetwin@yahoo.com
Sun, 22 Jul 2001 13:49:49 +0530
On Saturday 21 July 2001 23:33, D-man wrote:
> On Sat, Jul 21, 2001 at 04:02:08PM +0530, lonetwin@yahoo.com wrote:
> | Hi there,
> | One quick question, suppose I create a file (say 'foo.py'), that ha=
s
> | 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():
> | =09....
> | =09getattr([refer to this module], 'HisFoo')
> | =09....
> | how do I do that ?
>
> Why do you want to use the getattr function? You could simply say
>
> def main() :
> ...
> HisFoo
> ...
>
> since main and HisFoo are in the same module. That will be clearer
> and works just as well. I don't think there is any way around
> accessing a module-level variable like that. Inside a module there is
> the __name__ attribute that is a string containing the name of the
> module. You could use sys.modules or __import__ to get the module
> object based on the name, but you have to get the name by referencing
> a module object and you might as well just get that class directly.
>
Hi there D-man,
Thanx for replying....although I can't really use it like you said. Let =
me=20
explain, here's what I'm trying to do:
class Base:
=09....
=09....
=09....
class Sub1(Base):
=09ObjA =3D 'foo'
=09....
=09....
class Sub2(Base):
=09ObjA =3D 'bar'
def main():
=09....
=09....
=09x =3D funcReturnsSub1orSub2() # x can be either a Sub1 or a Sub2
=09y =3D getattr([refer to this module], x).ObjA
=09# y would be an ObjA irrespective of what x is, so y=3D'foo' | y=3D'ba=
r'
what I wrote above can be done from the prompt, like I showed in my previ=
ous=20
mail:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
[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'
>>> getattr(foo, 'HerFoo').x
'bar'
>>>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
where the file 'foo.py' contains:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
#!/usr/bin/python
=20
class MyFoo:
pass
class HisFoo:
x =3D 'foo'
class HerFoo:
x =3D 'bar'
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Please tell me there's a way to do this, else I'll have to rethink stuff,=
an'=20
thinking is not something that is happening naturally these days :) !!!
Peace
Steve
--=20
||||||||||||||||||||||
|||||||||#####||||||||
||||||||#######|||||||
||||||||# O O #|||||||
||||||||#\ ~ /#|||||||
||||||##||\_/||##|||||
|||||#||||||||||##||||
||||#||||||||||||##|||
||||#|||||||||||||##||=09
|||/\##|||||||||##/\||=09
|/ \#########/ \=09
|\ \#######/ /=09
||\____/#######\____/|=09
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=09
Debug is human, de-fix divine.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D