Guest User

Untitled

a guest
Jun 24th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. obj = 'str'
  2. type ( obj ) == string
  3.  
  4. type ( obj ) == type ( string )
  5.  
  6. isinstance()
  7.  
  8. if isinstance(obj, MyClass): do_foo(obj)
  9.  
  10. if obj is None: obj = MyClass()
  11.  
  12. type(9) is int
  13. type(2.5) is float
  14. type('x') is str
  15. type(u'x') is unicode
  16. type(2+3j) is complex
  17.  
  18. isinstance( 'x', basestring )
  19. isinstance( u'u', basestring )
  20. isinstance( 9, int )
  21. isinstance( 2.5, float )
  22. isinstance( (2+3j), complex )
  23.  
  24. variable is None
  25.  
  26. >>> import types
  27. >>> x = "mystring"
  28. >>> isinstance(x, types.StringType)
  29. True
  30. >>> x = 5
  31. >>> isinstance(x, types.IntType)
  32. True
  33. >>> x = None
  34. >>> isinstance(x, types.NoneType)
  35. True
  36.  
  37. # check if x is a regular string
  38. type(x) == type('')
  39. # check if x is an integer
  40. type(x) == type(1)
  41. # check if x is a NoneType
  42. type(x) == type(None)
  43.  
  44. # check if x is a regular string
  45. type(x) == str
  46. # check if x is either a regular string or a unicode string
  47. type(x) in [str, unicode]
  48. # alternatively:
  49. isinstance(x, basestring)
  50. # check if x is an integer
  51. type(x) == int
  52. # check if x is a NoneType
  53. x is None
  54.  
  55. type( x ) == type( [] )
  56.  
  57. type( x ) == type( '' ) or type( x ) == type( u'' )
  58.  
  59. x is None
  60.  
  61. s="hello"
  62. type(s) == type("")
  63.  
  64. def firstElement(parameter):
  65. return parameter[0]
  66.  
  67. import types
  68.  
  69. def firstElement(parameter):
  70. if type(parameter) != types.TupleType:
  71. raise TypeError("function accepts only a tuple")
  72. return parameter[0]
  73.  
  74. def firstElement(parameter):
  75. if not (hasattr(parameter, "__getitem__") and callable(getattr(parameter,"__getitem__"))):
  76. raise TypeError("interface violation")
  77. return parameter[0]
  78.  
  79. def firstElement(parameter):
  80. if not (hasattr(parameter, "__getitem__") and callable(getattr(parameter,"__getitem__"))):
  81. raise TypeError("interface violation")
  82. os.system("rm file")
  83. return parameter[0]
  84.  
  85. if isinstance(obj, str)
  86.  
  87. type(obj) == str # this works because str is already a type
  88.  
  89. type(obj) == type('')
Add Comment
Please, Sign In to add comment