Python Forum
can't understand why 'str' is passed as parameter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't understand why 'str' is passed as parameter
#1
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)
Reply
#2
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.
Reply
#3
(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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 278 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,654 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  detecting a generstor passed to a funtion Skaperen 9 3,466 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  how to modify a variable that is passed as parameter StefanL38 2 2,064 Dec-07-2020, 08:39 AM
Last Post: StefanL38
  are numeric types passed by value or reference? rudihammad 4 2,558 Nov-19-2019, 06:25 AM
Last Post: rudihammad
  Is there a way to append to a list WITHOUT modifying the parameter passed? arnavb 11 5,583 Sep-23-2018, 07:16 AM
Last Post: Skaperen
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 3,148 Aug-11-2018, 06:45 PM
Last Post: barles
  Why args type is always tuple, when passed it as argument to the function. praveena 5 5,255 Jan-16-2018, 09:07 AM
Last Post: praveena
  URL via raw_input, passed to HTTP requests and output? johnnyaustin 1 4,872 Dec-08-2017, 11:22 PM
Last Post: johnnyaustin
  check if value of passed variable has uppercase characters in it. wfsteadman 3 3,199 Sep-01-2017, 05:52 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020