Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- obj = 'str'
- type ( obj ) == string
- type ( obj ) == type ( string )
- isinstance()
- if isinstance(obj, MyClass): do_foo(obj)
- if obj is None: obj = MyClass()
- type(9) is int
- type(2.5) is float
- type('x') is str
- type(u'x') is unicode
- type(2+3j) is complex
- isinstance( 'x', basestring )
- isinstance( u'u', basestring )
- isinstance( 9, int )
- isinstance( 2.5, float )
- isinstance( (2+3j), complex )
- variable is None
- >>> import types
- >>> x = "mystring"
- >>> isinstance(x, types.StringType)
- True
- >>> x = 5
- >>> isinstance(x, types.IntType)
- True
- >>> x = None
- >>> isinstance(x, types.NoneType)
- True
- # check if x is a regular string
- type(x) == type('')
- # check if x is an integer
- type(x) == type(1)
- # check if x is a NoneType
- type(x) == type(None)
- # check if x is a regular string
- type(x) == str
- # check if x is either a regular string or a unicode string
- type(x) in [str, unicode]
- # alternatively:
- isinstance(x, basestring)
- # check if x is an integer
- type(x) == int
- # check if x is a NoneType
- x is None
- type( x ) == type( [] )
- type( x ) == type( '' ) or type( x ) == type( u'' )
- x is None
- s="hello"
- type(s) == type("")
- def firstElement(parameter):
- return parameter[0]
- import types
- def firstElement(parameter):
- if type(parameter) != types.TupleType:
- raise TypeError("function accepts only a tuple")
- return parameter[0]
- def firstElement(parameter):
- if not (hasattr(parameter, "__getitem__") and callable(getattr(parameter,"__getitem__"))):
- raise TypeError("interface violation")
- return parameter[0]
- def firstElement(parameter):
- if not (hasattr(parameter, "__getitem__") and callable(getattr(parameter,"__getitem__"))):
- raise TypeError("interface violation")
- os.system("rm file")
- return parameter[0]
- if isinstance(obj, str)
- type(obj) == str # this works because str is already a type
- type(obj) == type('')
Add Comment
Please, Sign In to add comment