[Tutor] Name of a module within itself
lonetwin@yahoo.com
steve@in.cqsl.com
Sun, 22 Jul 2001 15:31:02 +0530 (IST)
On Sun, 22 Jul 2001, steve wrote:
>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 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 ?
>>
>> Why do you want to use the getattr function? You could simply say
>>
>> def main() :
>> ...
>> HisFoo
>> ...
>> <..snip..>
>Hi there D-man,
> Thanx for replying....although I can't really use it like you said. Let me
>explain, here's what I'm trying to do:
>
>class Base:
> ....
>
>class Sub1(Base):
> ObjA = 'foo'
> ....
>
>class Sub2(Base):
> ObjA = 'bar'
>
>def main():
> ....
> x = funcReturnsSub1orSub2() # x can be either a Sub1 or a Sub2
>
> y = getattr([refer to this module], x).ObjA
> # y would be an ObjA irrespective of what x is, so y='foo' | y='bar'
>
> <..snip..>
Hi everybody,
Yet again, I've proved I go around looking for complex solutions to simple
problems <donks himself> donk donk donk !!! well, here's what I am happily doing
now:
the funcReturnsSub1orSub2() (..refer above..) used to return a string 'Sub1'
or 'Sub2'(....how immensely dumb !!!...), when all that I needed was an actual Sub1
or Sub2, so, now instead of:
x = funcReturnsSub1orSub2()
y = getattr([refer to this module], x).ObjA
I do:
x = funcReturnsSub1orSub2()
y = x.ObjA
<ducks and runs for cover from all kinds of *objects* being hurled at him>
Sorry D-man, I think I wasted your time on this one :D
When will I learn !!!!
Peace
Steve
--
||||||||||||||||||||||
|||||||||#####||||||||
||||||||#######|||||||
||||||||# O O #|||||||
||||||||#\ ~ /#|||||||
||||||##||\_/||##|||||
|||||#||||||||||##||||
||||#||||||||||||##|||
||||#|||||||||||||##||
|||/\##|||||||||##/\||
|/ \#########/ \
|\ \#######/ /
||\____/#######\____/|
=====================================
Debug is human, de-fix divine.
====================================