Python - Access a function above outside a class

Asked

Viewed 889 times

0

Hey there, guys! A question regarding Python.

I have a structure more or less like this:

class Tela(FloatLayout):
   def exemplo(self):
      print("teste")

   class botao(Button):
      ?????

Explaining: It is a screen that has a function and a class. I wanted to know how to access the function that is in the class "Canvas" within the class "boot". I thought it might be something like: variable = Canvas.example() or root.example(), but none of that worked. How is it done this?

  • Is the class button set within the screen class? If so, why?

  • Hi Anderson! So as I’m a beginner, I actually did it more for the sake of getting organized. :)

1 answer

1

There is no gain or reason to define classes within Python classes. (Ok, there’s actually a very minor reason: you might want to use your classes as namespaces to maintain values)

But in this case, you get nothing. What your "Canvas" will need to have are instances of "Boot" the instances you create when creating your instance of "Canvas" - but I repeat: there is no gain, no benefit, nothing, nothing to define one class within the other. On the other hand, doing so will work against language, and various things under the hood that are there to help you start working against. Not being able, from within the nested class, to access the above class is just one of those things.

So, what should you do? Keep attributes inside your classes that connect them to the related objects - for example: a button will need to know which screen it is on - so it should receive this information when created.

Look how simple it gets:

class Botao:
   def __init__(self, tela):
       self.tela = tela
       ...
   def acao(self):
       self.tela.acao_global()

class Tela:
   def __init__(self):
        self.botao_ok = Botao(self)
        self.botao_cancela = Botao(self)

   def acao_global(self):
       # chamada quando o método "acao" de qualquer botao é executado
  • Wow!! Great explanation, @jsbueno, thank you for the mini lesson, Hehe! I get what you mean. Actually I had a nice Python base, but as a beginner, I still skips on some things that are conceptual! But thank you!! Hug!

  • is - sorry if I insisted so much on the idea of not using nested classes - guess you can understand when I first speak. But I’ve already caught people who insist on doing this, and when I write something here, I always write thinking that more people, besides who asked the original question, will read.

  • No, for sure @jsbueno! Man, I always have for myself that programming you always have to have an open mind and learn as much as the other people who talk. I even got used to thinking this way in the "nested" programming. But I really liked what you advised! I just need to change my way of thinking at the time of programming, but I really liked it! I appreciate it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.