0

How to check which datatype does variable contain in python.

Like if I am creating a function.I want to check which datatype of argument is given how can i check it.

28th Jan 2021, 9:34 AM
Vinay Jaiswal
Vinay Jaiswal - avatar
9 ответов
+ 2
Vinay Jaiswal Remove the parentheses from list(): if type(x) is list:
28th Jan 2021, 10:02 AM
jtrh
jtrh - avatar
+ 5
foo = 42 bar = 'forty-two' print(type(foo)) print(type(bar))
28th Jan 2021, 9:37 AM
visph
visph - avatar
+ 5
Also, you can check it using isinstance (variable, datatype) More about isinstance in https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_func_isinstance.asp
28th Jan 2021, 11:26 AM
Adamyan
Adamyan - avatar
+ 2
as per Seg Adamyan post, but you need to be very specific var = 2 print(isinstance(var,int)) print(isinstance(var,str))
28th Jan 2021, 11:43 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Vinay Jaiswal Try this as per visph suggestion def define(x): return type(x) print(define(2)) print(define('abc')) print(define([2,3,'a','b'])) PS: Had a glitch before, sorry if strange message got sent
28th Jan 2021, 10:13 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
This will not work visph.
28th Jan 2021, 9:51 AM
Vinay Jaiswal
Vinay Jaiswal - avatar
0
Like. "def check(x): if type(x) is list(): print("Is of type list") else: print("This is of another type")" I know logic is wrong.But which logic i apply.
28th Jan 2021, 9:55 AM
Vinay Jaiswal
Vinay Jaiswal - avatar
0
type() function can be used to determine which data type class
29th Jan 2021, 6:03 PM
Vicktoya Bentley
Vicktoya Bentley - avatar