Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
python - Get input argument's name of function

How can I get name of input argument in function?

Here is example

a=1
b=2
c=3
def test(x):
    print(?)

test(b)

and result should be

b

I think this is easy problem...? But not to me :)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Due to the fact that multiple variables can have the same value, the below function returns a list of all the variable names that holds the value passed in as argument:

a = 1
b = 2
c = 3
def test(x):
    print([k for k, v in globals().items() if v == x])

test(b)

Output:

['b']

Of course, you can always return after the first occurrence of a match:

a = 1
b = 2
c = 3
def test(x):
    for k, v in globals().items():
        if v == x:
            return print(k)

test(b)

Output:

b
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda