How to declare and use methods that are in the same class

1

Good afternoon. I would like to know how to declare and use methods within the same class. For example, in a class there are 2 methods, which 1 method uses the other.

For example:

class ClasseEMetodo:
    def retorna_valor:
        return '10'

    def imprimi_valor:
        print('O valor é {0}' retorna_valor())


# chamada
var = ClasseEMetodo
var.imprimi_valor()

The error shown is:

    def retorna_valor:
                     ^
SyntaxError: invalid syntax
    
asked by anonymous 17.05.2018 / 20:17

1 answer

1
class ClasseEMetodo:
    @classmethod
    def retorna_valor(cls):
        return '10'

    @classmethod
    def imprimi_valor(cls):
        print('O valor é {0}'.format(cls.retorna_valor()))


# chamada
var = ClasseEMetodo
var.imprimi_valor()  # O valor é 10
    
17.05.2018 / 20:25