Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Get importer module from imported module
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="dody suria wijaya, post: 1803863"] Yup, Got it. New problem though. On line: newclass = new.classobj(classname + '_' + food, (mixin, main), {}) received "TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases". This had not happened when was written using 'normal' style like: class (SaltyMixIn, Main): pass -- dody suria wijaya YahooMsgr ID: dody Monday, February 7, 2005, 7:20:14 PM, you wrote: DB> Ok, I understand now what you are trying to DB> achieve, but there isn't any DB> concept relating a module back to the first module which tried to import DB> it. Instead you'll have to specify the relationship yourself. DB> How about this: DB> import sys DB> food_no = sys.argv[1] DB> m = __import__(food_no) DB> goodie = b.Salty(m) DB> ... DB> def Salty(module): DB> class Food(SaltyMixIn,module.Main): pass DB> return Food() DB> ... DB> In fact, it looks like the importing isn't really DB> something module c should DB> be concerned about. I haven't tested the following code (so it will have DB> bugs), but it should give you some ideas. It remembers classes it has DB> created so as not to duplicate them, and it also gives each class a DB> sensible name. DB> import sys DB> food_no = sys.argv[1] DB> goodie = b.Salty(food_no) DB> ... DB> import new DB> Classes = {} DB> def makeFood(classname, food, mixin): DB> m = __import__(food) DB> main = m.Main DB> if (mixin, main) in Classes: DB> return Classes[mixin, main]() DB> newclass = new.classobj(classname + '_' + food, (mixin, main), {}) DB> Classes[mixin, main] = newclass DB> return newclass DB> def Salty(food): DB> return makeFood('Salty', food, SaltyMixIn) DB> ... [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Get importer module from imported module
Top