How from the following function to get the variable login and password from another module?

def logpass(message): logp = message.text.split(':') login = logp[0] password = logp[1] 

The module in which the login and password take the value is called bot.py, the module in which are called example.py. Give an example using

 print() 

I do not know whether the return is appropriate, since the function is not called explicitly. I use decorators from telebot

 @bot.message_handler(content_types = ['text']) def logpass(message): 
  • login = bot.login ? - MaxU
  • @MaxU Does not find this variable, apparently due to the fact that it is in the function - r4d1f
  • those. logpass() declared in bot.py ??? - MaxU
  • @MaxU exactly .... - r4d1f
  • if login and password defined only in the function, then I think not at all, and if they are also defined outside the function, then bot.login - MaxU

1 answer 1

In general, it is very funny, but it seems you can:

 code = bot.logpass.function_code index = code.co_varnames.index('login') value = code.co_consts(index + 1) 

Without guarantees))

That is, there is a theoretical possibility in Python to get the value of the LOCAL variable of the function OUTSIDE the function.

 def f(): x = 1 y = 2 code = f.function_code print (code.co_varnames) # -> ('x', 'y') print (code.co_consts) # -> (None, 1, 2) 
  • can you give a complete example? function_code , co_varnames are inspect attributes? - MaxU
  • @MaxU function_code - attribute (as I understand it) of any function - andy.37
  • hmmm, in Python 3.5 it doesn't work: AttributeError: 'function' object has no attribute 'function_code' - MaxU
  • @MaxU I was played in the 2nd. For the 3rd I will not say. To be honest, I learned about the existence of this exactly 5 minutes ago))) - andy.37
  • @MaxU in python3.x instead of f.function_code you need to write f.__code__ - andy.37