0

Pls how do i check multiple type(s)

Let's say i have five variables; a, b, c, d and e and i want to check the type of each of them without typing it fully; i.e print(type(a)); print(type(b));....print(type(e)) or print(type(a), type(b),... type(e)) I tried - print(type(a,b...e)) but it failed

28th Jun 2021, 2:09 PM
Nwalozie Elijah
Nwalozie Elijah - avatar
5 Respostas
+ 1
Mix the various variables in an iterable, and then use a loop to print each element in the iterable a, b, c, d, e = 42, 'hello', True, 20.21, [ 1, 2, 3, 4, 5 ] # variables mixed_type = ( a, b, c, d, e ) # mixed in a tuple for e in mixed_type: print( f"{e} is {type( e )}" ) # print each tuple element There may be better ways, it's just what I had in mind.
28th Jun 2021, 2:27 PM
Ipang
+ 1
Ipang Thanks. I'm the not really looking to put it in a loop though.
28th Jun 2021, 2:32 PM
Nwalozie Elijah
Nwalozie Elijah - avatar
+ 1
How about using generator, converted to `list`? print( *list( type( e ) for e in ( a, b, c, d, e ) ), sep = '\n' )
28th Jun 2021, 3:08 PM
Ipang
+ 1
Ipang yh this makes sense. I prefer this. I was really hoping the type() function could take multiple variables at once. Maybe that's not the case 😩
28th Jun 2021, 3:21 PM
Nwalozie Elijah
Nwalozie Elijah - avatar
+ 1
The type() function does have an overload that takes 3 arguments. But it serves different purpose, as you may see here https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/built-in/type
28th Jun 2021, 3:40 PM
Ipang