mardi 19 octobre 2021

Python extract variable in if statement and have checks

Is the below code valid in python or its not possible to extract variable in python and perform checks on it in one line?


def method1(self,some_text):
   """For illustration purpose it returns a hard coded value"""
   return (1,2)

if x = method1("Hello") and x[0] == 1 and x[1] = 2:
   print("This is it {},{}".format(x[0],x[1]))
elif x = method1("World") and x[0] == 3 and x[1] = 4:
   print("This is it, second {},{}".format(x[0],x[1]))
elif x = method1("The") and x[0] == 5 and x[1] = 6:
   print("This is it, third {},{}".format(x[0],x[1]))
else:
   print("No match")

Currently I have this if condition for multiple attribute to check. Since python throws error because its not a valid syntax, I have to declare all such property above the if statement and so many attributes being declared in the local block.

Any better way or suggestion, to have extract the variable and have checks in one line as well as use the attribute within if block?

Aucun commentaire:

Enregistrer un commentaire