OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Is there a way to call a variable that is inside a function in Python?

  • Thread starter Thread starter Bruno Ciccarino
  • Start date Start date
B

Bruno Ciccarino

Guest
I recently started studying Python, because of job opportunities, so today I decided to do an exercise using what I've been learning, I'm creating a bot for Telegram in Python, then I came across this situation: I need to call a variable that is in another file to the main file. to make the code cleaner, I separated the code into several files, there is a file to call the api, another that is the main file and another with the bot commands, to To be able to call the variables from the api, I created a function, but I don't know if I can call the variables from within this function:

Code:
def botApi(SHINEAPI, bot):
    SHINEAPI = decrypt_api_key()
    bot = telebot.TeleBot(SHINEAPI)
    return botApi

And I need to call the bot variable for this decorator:

Code:
@bot.message_handler(commands=['start'])
def respostaPadrao(mensagem):
    ....

Does anyone know if there is a way?

I've already tried to ask my technical course teacher and he still hasn't answered and I've tried searching but I haven't found anything that objectively answers my question... and I also tried to declare it as a global variable but it gave an error. I forgot to mention that I had also tried to call it as a return function, but I couldn't call the variable, only the function and even if I tried to pass the variable as a parameter it wouldn't work...
<p>I recently started studying Python, because of job opportunities, so today I decided to do an exercise using what I've been learning, I'm creating a bot for Telegram in Python, then I came across this situation: I need to call a variable that is in another file to the main file. to make the code cleaner, I separated the code into several files, there is a file to call the api, another that is the main file and another with the bot commands, to To be able to call the variables from the api, I created a function, but I don't know if I can call the variables from within this function:</p>
<pre><code>def botApi(SHINEAPI, bot):
SHINEAPI = decrypt_api_key()
bot = telebot.TeleBot(SHINEAPI)
return botApi
</code></pre>
<p>And I need to call the bot variable for this decorator:</p>
<pre><code>@bot.message_handler(commands=['start'])
def respostaPadrao(mensagem):
....
</code></pre>
<p>Does anyone know if there is a way?</p>
<p>I've already tried to ask my technical course teacher and he still hasn't answered and I've tried searching but I haven't found anything that objectively answers my question... and I also tried to declare it as a global variable but it gave an error. I forgot to mention that I had also tried to call it as a return function, but I couldn't call the variable, only the function and even if I tried to pass the variable as a parameter it wouldn't work...</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top