Get importer module from imported module
dody suria wijaya
dodysw at gmail.com
Tue Feb 8 00:01:02 EST 2005
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> dody suria wijaya wrote:
>> "import a" inside b would not solve the problem, since there
>> are many "module a" and module b does not know beforehand
>> which module had imported it.
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:
>> #Module c
DB> import sys
DB> food_no = sys.argv[1]
DB> m = __import__(food_no)
DB> goodie = b.Salty(m)
>> #Module b
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.
>> #Module c
DB> import sys
DB> food_no = sys.argv[1]
DB> goodie = b.Salty(food_no)
>> #Module b
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> ...
More information about the Python-list
mailing list