accessing a classes code

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Apr 20 21:33:13 EDT 2006


John Machin a écrit :
> On 20/04/2006 6:54 AM, bruno de chez modulix en face wrote:
> 
>>> It turns out that what I want to do can be done using the inspect
>>> module which has methods for getsourcecode among other things.
>>
>>
>> I never said that what you wanted to do was impossible (nor even
>> difficult, and FWIW, there are simpler alternatives than using inspect
>> - using a templating system like empy comes to mind...). I only suggest
>> that there are possibly far better solutions, that you seem to dismiss
>> for some unknown reason...
>>
> 
> I'm with bruno -- IMHO, the OP's "solution" seems Byzantine, bizarre, ...
> 
> Suggestion: the initial investigator gadget writes out the discovered 
> parameters into a case-specific config file, whose name is later passed 
> as an arg to the [only one] class's __init__ method()

Suggestion: the investigator gadget is imported and called at the 
top-level of the modules containing the classes, that can then access 
the needed values. Then it's always possible to implement any caching 
mechanism in the investigator itself.

import investigator
platform_params = investigator.investigate()

class MyObj(object):
   def __init__(self):
     self.attr1 = platform_params.attr1
     self.attr2 = platform_params.attr2
     (...)
     self.attrN = platform_params.attrN

  myMethod = platform_params.myMethod

  def otherMethod(self, arg):
    return platform_params.otherMethod(self, args)




More information about the Python-list mailing list