1

Can anyone help me in understanding what is the use of plus(+) sign in using isinstance.

In [76]: isinstance('qwert', string)
Out[76]: True

In [77]: isinstance('qwert', string + (int,))
Out[77]: True

Especially, this part--> string + (int,)

xlax
  • 2,661
  • 6
  • 12
  • 15

1 Answers1

4

Assuming that your variable string_types is itself a tuple of string types (e.g. (str, bytes,)), the + operator is concatenating the string_types tuple and the (int,) tuple together into something like (str, bytes, int,)

  • @xlax I cannot understand. Can you tell more detail about how it works or give some references? Why out[77] returns True? – Zealseeker Mar 23 '17 at 06:35
  • `string` will not work. you would need to import `string_types` from `six` ..and write `string_types` instead of `string`. – xlax Mar 23 '17 at 07:36
  • we are checking whether or not the given input i.e 'qwert' is a "string or int" ( string_types + (int,) = (basestring, int) ). since the input is a string, it will return True. `In [78]: string_types + (int,) Out[78]: (basestring, int)` – xlax Mar 23 '17 at 07:40