so here's what I want to do:
import module
mvar = module()
I've seen it done with the PyQt modules,
but I don't feel like researching a complex code just to figure out a simple usage. :P
anyone know how this works??
so here's what I want to do:
import module
mvar = module()
I've seen it done with the PyQt modules,
but I don't feel like researching a complex code just to figure out a simple usage. :P
anyone know how this works??
Normally, you can't do that and you should not be doing that. However this is python and most things happen to be possible. A trick is to replace the module in sys.modules
by a pseudo module object with the same dict and a __call__()
method. Here is module lemodule.py
in python 2.7:
# -*- coding: utf-8 -*-
"""
lemodule.py
"""
if __name__ != '__main__':
import sys
class PseudoModule(object):
def __init__(self, dic):
self.__dict__ = dic
def __call__(self):
return 3.14159
sys.modules[__name__] = PseudoModule(
sys.modules[__name__].__dict__)
Now here is the program main.py which does what you want
# -*- coding: utf-8 -*-
"""
main.py
"""
import lemodule
var = lemodule()
print(var)
"""my output -->
3.14159
"""
solved: http://python-forum.org/posting.php?mode=reply&f=6&t=10867
EDIT: oh thanks Gribouillis
I wasn't notified in time about your reply.
(and still have yet to be notified)
I see that Alex Martelli's solution is the same as mine. This is very encouraging.
lol IKR :P
yours is slightly better though for the class init WITH the module dict.
very nice :)
oh god dangit...
I just realized the new link I posted didn't save as the new link...
not sure what happened there, but it should be this:
http://python-forum.org/viewtopic.php?f=6&t=10867
(the current link takes me to the reply box)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.