T
T
TanderOFF2021-08-01 15:54:26
Python
TanderOFF, 2021-08-01 15:54:26

How to use a variable from another file?

Hello, I use cogs, I would like to use a variable from the main file in the cogs folder in the voice.py file?

Main file: main.py

@client.event
async def on_ready():
    global tdict
    tdict = {}


cogs folder, voice.py file:

class voice(commands.Cog):
    def __init__(self, client):
        self.client = client


    @commands.Cog.listener()
    async def on_voice_state_update(self, member, before, after):
        print(tdict) #как использовать эту переменную тут ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-01
@Vindicar

The simplest way is to pass the dictionary to the constructor.

class voice(commands.Cog):
    #при создании кога передаёшь ему клиента и tdict
    def __init__(self, client, t_dict):
        self.client = client
        self.tdict = t_dict

    @commands.Cog.listener()
    async def on_voice_state_update(self, member, before, after):
        print(self.tdict)

Keep in mind, in the case of reference objects, i.e. dictionaries, lists, etc., changing an object inside of it will change it globally. But value objects (numbers, strings, tuples) cannot be changed in this way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question