0
It may not have been very clear about the title, but I wanted it only to be possible to use a method on an object if one or more of its attributes are in a certain way. For example: A TV class that has several methods, to increase, decrease, change channel, turn on, turn off. As the code below shows (in this case I removed all methods and attributes that do not help in understanding doubt):
I removed everything from the code that helped in the understanding of doubt
'''python
class Tv:
def __self__(self, ligado=False, volume=0):#apenas define os atributos
self.ligado = ligado
self.volume = volume
def altera_volume(self, novo_volume):#ele so faz mudar o valor do atributo "volume"
self.volume = novo_volume
def botao_on(self):#ou seja, esta meio que precionando aquele botaozinho de ligar a tv
if self.ligado == False:#ou seja se a tv estiver desligada, ele vai ligar ela. ou seja "ligado" sera verdadeiro
self.ligado == True
else:#ou seja, se a tv estiver ligada o atributo ligado será falso
self.ligado == False
#Nada do que esta escrito dentro dos metodos "altera_volume" e "botao_on" importa, a unica coisa que importa é que "altera_volume" só
#pode ser usado se o atributo "ligado" for verdadeiro, ou seja olha o que aconteceria abaixo
televisao = Tv()#ou seja televisao é uma Tv desligada com volume 0
televisao.alterar_volume(100)#Aqui esta o problema, como é possivel alterar o volume com a tv desligada?
#Tem alguma forma de proibir o uso do metodo "alterar_volume" até que o atributo "ligado" tenha o valor que eu quero?
'''
Actually I wanted it to be possible to use the "altera_volume" method only if the attribute "turned on" had a true value. I could even put an IF to check this, but if they had many methods (like 50 or 100) it would be difficult to put an IF for each of them. Dai wondered if there’s any more practical way to do it
– Cachorro_louco
Voce can put the if in the volume alters method and call it from the others. In volume change you check if this connected then if it is applied the volume change.
– marcos paulo silva viana
In def botao_on you return the state self on.:
def botao_on(self):#ou seja, esta meio que precionando aquele botaozinho de ligar a tv
 if self.ligado == False:#ou seja se a tv estiver desligada, ele vai ligar ela. ou seja "ligado" sera verdadeiro
 self.ligado == True
 else:#ou seja, se a tv estiver ligada o atributo ligado será falso
 self.ligado == False return self.ligado
– marcos paulo silva viana
In this case would you need to put an IF on all methods to verify this? because if it had several methods, like "muda_canal", "poem_mudo", "restarts"... I would need to check in each method. or even if there were many more methods.
– Cachorro_louco
If you have to check yes, but the difference is that vc will return the value of the state so that everyone sees with Return self.on, but it could also nest with the if case on and thus perform the other functions. In the case of the connected tv the volume and other functions would be activated automatically or would have a command ?
– marcos paulo silva viana
Assuming this program was run, then it would enter interactive mode. I could instantiate another object, but if the linked attribute is with False value I would still be able to use its methods. How could you do a check. The problem is that there could be many methods and having to create a check for each of the methods would be complicated. But the methods would not be activated automatically, only if you write. Actually I don’t even know how to automatically activate
– Cachorro_louco