How do I instantiate a class using variables coming from a function return in Python 3?

0

Hello, I would like to be able to instantiate the class Sala below using as arguments the return of the choose_sala() function: The ideal would be to call on the return of the function the initialization of class Sala , but I did not succeed so far.

The simplified example follows:

Python 3

class Sala:
    def __init__(self, ano, turma):
        self.ano = ano
        self.turma = turma

        print(" Sala Escolhida foi... ")

    def say_hello:
        print("Hello!")

r1 = [] # desnecessário!

def choose_sala():
    r1 = ['8A']
    r1 = "".join(r1)
    print(r1)

    print(r1[0], " ", r1[1]) #para poder usar casa letra separadamente, não deu certo usando a lista normal.
    return r1

I wish I could use:

a = Sala(choose_sala())
a.say_hello() # teste da classe
    
asked by anonymous 19.07.2018 / 21:00

1 answer

0

What you want to do is:

a = Sala(*choose_sala())

The * causes each returned element to be passed separately as a separate parameter for the Sala class.

See more about this in the official basic tutorial here .

    
19.07.2018 / 21:07