0

I have an object of class and want to re initialize the object by a function

So I am writing a discord bot and there's one object that i want to be recreated every time the bot gets a command.

import discord
client = discord.Client()
obJ_class = bot_friendly.Exec() # this creates the object and everything works fine

now

@client.event
async def on_message(message):
    if message.content.startswith("&&exit"):
    await message.channel.send("Exiting")
    await client.close()
    print("Successfully logged out")
    if message.content.startswith("&&update"):
    await message.channel.send("Wait a few seconds")
    obJ_class = bot_friendly.Exec()

here the error is Shadows name 'obJ_class' from outer scope

I would like to know if this is even doable or should i just make another function that does what the __init__(self): does but just name it update():

  • You need the ``global`` keyword to modify global names from inside a function. Declare ``global obJ_class`` inside ``on_message`` and the assignment modifies the global ``obJ_class`` variable. – MisterMiyagi Sep 22 '20 at 15:33
  • Thank you so very much, at first i tried declaring it as `global obJ_class` but i did it right under `obJ_class = bot_friendly.Exec()` and not inside the method. Now it works perfectly. – user9541267 Sep 22 '20 at 16:45

0 Answers0