1

Is it possible to access a class attribute in a python decorator?

Code:

class ButtonView(discord.ui.View):
  def __init__(self, allPages, author, *, timeout = 180):
    super().__init__(timeout=timeout)
    self.allPages = allPages

  @discord.ui.button(label=self.allPages, style=discord.ButtonStyle.secondary, custom_id="current_page_button")
  async def current_page_button_callback(self, button, interaction):
    ...

I get a warning on self.allPages in @discord.ui.button():

self.allPages is not defined

Mongonesa
  • 35
  • 3
  • The decorator is applied *to the function, when the class is being created*, so there is no `self` from which the attributes could be accessed. – Karl Knechtel Jul 13 '22 at 13:25
  • Okay, thanks! I managed to overcome this by using the discord.ui.Button and made a few changes. The code is now a little bit more complicated and not as elegant looking, but it does the job! – Mongonesa Jul 13 '22 at 21:07

0 Answers0