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
  Extract args=, value that was passed to Multiprocessing.Proc object during runtime? haihal 1 611 Dec-08-2024, 07:04 AM
Last Post: Gribouillis
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,257 May-13-2024, 05:57 AM
Last Post: Larz60+
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 1,237 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 17,374 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  detecting a generstor passed to a funtion Skaperen 9 5,122 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  how to modify a variable that is passed as parameter StefanL38 2 2,735 Dec-07-2020, 08:39 AM
Last Post: StefanL38
  are numeric types passed by value or reference? rudihammad 4 3,466 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 7,780 Sep-23-2018, 07:16 AM
Last Post: Skaperen
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 4,017 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 6,484 Jan-16-2018, 09:07 AM
Last Post: praveena

Forum Jump:

User Panel Messages

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