I decorated some methods with @bot_thinking
, which stores some information about the decorated method in the functions
attribute.
One piece of information is 'class_name', but my program needs the class type as a variable, e.g. RandomBot
. I would like to get this class.
Here is some sample code:
class DepthPrunedMinimaxAgent(Agent):
@bot_thinking(associated_name="minimax profondeur")
def select_move(self, game_state: GameState):
Above is the decorated part of the code.
The decorator:
functions = {}
def bot_thinking(associated_name, active=True):
def _(func):
if active:
class_name = func.__qualname__.rsplit('.')[-2]
import sys
# class_name_2=getattr(sys.modules[__name__], class_name)
# module=importlib.import_module('sources.agent')
functions[associated_name] = (associated_name, class_name,
globals()[class_name], func)
else:
functions.pop(associated_name)
return _
bot_thinking
isn't a real decorator, it's a decorator factory.
From the func
function, I get the class_name
, but I can't use the accepted answer by @m.kocikowski, to find the correct class because this class is decorated, so it already imports the annotation module, so importing from the module of the annotation the annotated module would result in a cyclic import, which python does not seem to permit.
Do you see a method to get the class from its name?
ps: ps: to be clearer : the annotation part of the code need an import to the annotated classes(to retrieve the class from its name), which also need an importation of the annotation (for the annotation to work).