Python Forum

Full Version: can't understand why 'str' is passed as parameter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just want to know why str keyword is passed in isinstance() method?

def to_str(data):
  if isinstance(data, str):
    return data
  elif isinstance(data, bytes):
    return data.decode(‘utf-8’)
  else:
    raise TypeError(‘Must supply str or bytes, ‘ ‘found: %r’ % data)
I added code tags for you, read about bbcode
and use it on future posts
isinstance asks if the first argument is of the second arguments type,
in other words if data is a string then return it.
(Mar-09-2018, 04:57 AM)uddhavpgautam Wrote: [ -> ]str keyword
str isn't a keyword, it's a type. isinstance's second parameter, is a type (or class).