â»Python 2.7
Pythonã§åã調ã¹ãæ¹æ³ã¯ç°¡åã§ãisinstanceã¨ããçµã¿è¾¼ã¿é¢æ°ã使ç¨ãã¾ãã
listãã©ããã調ã¹ãéã¯ã
if isinstance([], list): print "this is list type." if isinstance(1, int): print "this is integer type."
ã¨ãããµãã«ãã¾ãã
listãintã¯ãªãã¸ã§ã¯ãã§ããã[]ã1ã¯ããããã®ãªãã¸ã§ã¯ãã®ã¤ã³ã¹ã¿ã³ã¹ã§ãã
Pythonã§ã¯é¢æ°ã®å¼æ°ã¯åãéå®ããªãï¼å ¨ã¦ãªãã¸ã§ã¯ããªã®ã§ï¼ä½¿ãæ¹ãã§ããã®ã§ãåã調ã¹ããã¨ã§å¼æ°ã«å¿ãã¦å¦çãåããã¨ããä»çµã¿ãå®ç¾åºæ¥ã¾ãã
ããã§ã¯ãé¢æ°ãªãã¸ã§ã¯ãã®ååã¯ä½ã§ããããï¼ã¾ããã¯ã©ã¹ã®ååã¯ä½ã§ããããï¼ããã¯typeé¢æ°ã使ç¨ãããã¨ã§èª¿ã¹ããã¨ãã§ãã¾ãã
Pythonã§ã¯å ¨ã¦ã®å¤æ°ãé¢æ°ã¯ãªãã¸ã§ã¯ããªã®ã§ãå¼æ°ã¨ãã¦æ¸¡ããã¨ãã§ããã®ã§ããããã®éã«æ¸¡ã£ãå¼æ°ã®åã調ã¹ã¦å¦çãåããã¨ãã«ã¯ãåã®ç¨®é¡ãç¥ããªãã¦ã¯ãªãã¾ãããåãä½ãã調ã¹ãéã«ã¯typeé¢æ°ã使ç¨ãã¾ãã
typeé¢æ°ã§ããããªãªãã¸ã§ã¯ãã®åã調ã¹ããµã³ãã«ãæ¸ãã¾ããã
#!/usr/bin/python # -*- coding:utf-8 -*- import os class OwnClass(): def __init__(self): self.param = 123 def func(self): print self.param return True if __name__ == '__main__': owc = OwnClass() print "string 's' is '%s'" % (type("s")) print "integer 1 is '%s'" % (type(1)) print "float 0.1 is '%s'" % (type(0.1)) print "float 1.0e-3 is '%s'" % (type(1.0e-3)) print "list [] is '%s'" % (type([])) print "tuple () is '%s'" % (type(())) print "None is '%s'" % (type(None)) print "function 'int' is '%s'" % (type(int)) print "function 'type' is '%s'" % (type(type)) print "module 'os' is '%s'" % (type(os)) print "member 'os.path.sep' is '%s'" % (type(os.path.sep)) print "method 'os.path.exists' is '%s'" % (type(os.path.exists)) print "my class 'owc' is '%s'" % (type(owc)) print "my member 'owc.param' is '%s'" % (type(owc.param)) print "my method 'owc.func' is '%s'" % (type(owc.func))
åºåã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
string 's' is '<type 'str'>' integer 1 is '<type 'int'>' float 0.1 is '<type 'float'>' float 1.0e-3 is '<type 'float'>' list [] is '<type 'list'>' tuple () is '<type 'tuple'>' None is '<type 'NoneType'>' function 'int' is '<type 'type'>' function 'type' is '<type 'type'>' module 'os' is '<type 'module'>' member 'os.path.sep' is '<type 'str'>' method 'os.path.exists' is '<type 'function'>' my class 'owc' is '<type 'instance'>' my member 'owc.param' is '<type 'int'>' my method 'owc.func' is '<type 'instancemethod'>'