Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] What’s the Standard Way to Check for Type in Python?

#1
What’s the Standard Way to Check for Type in Python?

Problem: What is the best way to check whether a given object is of a given type? For example, if you have a variable object, how to check if it’s a string?

Check with Subclasses


Solution: To check if an object is an instance of str or any of its subclasses, use isinstance:

if isinstance(o, str): print(o, 'is of type string')

Note that if the object inherits from string, the result of isinstance(o, str) will also be True.

Here’s an example:

class Car: def speed(self): return 100 class Sportscar(Car): def speed(self): return 200 vw = Car()
porsche = Sportscar() print(isinstance(vw, Car))
# True print(isinstance(porsche, Sportscar))
# True print(isinstance(porsche, Car))
# True --> Inherit being a Car from being a Sportscar

The example shows that the vw is a Car and the porsche is a Sportscar. However, as a Sportscar inherits from Car, the porsche is also a Car.

However, you could argue that checking for type shouldn’t include checking for subclasses. So, in this example, the porsche instance should not be a Car. Let’s have a look how to accomplish this next!

Check without Subclasses


Solution 2: To check if an object is an instance of str, excluding subclasses, use Python’s built-in function type(object) and check if it’s the same as str using the keyword is.

obj = 'hello' if type(obj) is str: print(obj, 'is of type string (it is not a subclass)') 

The output is:

hello is of type string (it is not a subclass)

Similarly, if you use the same class example as before, the porsche would now not be a Car—even though it’s a Sportscar!

class Car: def speed(self): return 100 class Sportscar(Car): def speed(self): return 200 vw = Car()
porsche = Sportscar() print(type(vw) is Car)
# True print(type(porsche) is Sportscar)
# True print(type(porsche) is Car)
# False --> Inherit being a Car from being a Sportscar but is not of type Car

Resources: You may also enjoy to check out the following resources about Python typing:

The post What’s the Standard Way to Check for Type in Python? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Compare Rpm Versions in Python? xSicKxBot 0 13 03-01-2021, 07:10 PM
Last Post: xSicKxBot
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 24 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 40 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 32 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 40 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 35 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 37 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] How to Check if an Object is of Type List in Python? xSicKxBot 0 33 02-09-2021, 08:05 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 45 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How to Send UDP Multicast in Python? xSicKxBot 0 38 02-03-2021, 01:31 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] Best LLC Services for Your Freelan...
Last Post: xSicKxBot
Today 07:38 PM
» Replies: 0
» Views: 1
(Indie Deal) PAW Patrol, Ben10, Dead in ...
Last Post: xSicKxBot
Today 07:38 PM
» Replies: 0
» Views: 2
(Free Game Key) Wargame: Red Dragon - Fr...
Last Post: xSicKxBot
Today 07:38 PM
» Replies: 0
» Views: 1
Flutter 2 Release Adds Desktop and Web S...
Last Post: xSicKxBot
Today 07:38 PM
» Replies: 0
» Views: 3
Microsoft - Meet the 2021 Imagine Cup Wo...
Last Post: xSicKxBot
Today 07:38 PM
» Replies: 0
» Views: 1
News - Review: G-Darius HD – A True Shmu...
Last Post: xSicKxBot
Today 07:37 PM
» Replies: 0
» Views: 0
News - Minute Of Islands, The Adventure ...
Last Post: xSicKxBot
Today 07:37 PM
» Replies: 0
» Views: 0
News - Tales From the (Deep Stone) Crypt
Last Post: xSicKxBot
Today 10:30 AM
» Replies: 0
» Views: 6
Xbox Wire - The Ford F-150 RLE Has Arriv...
Last Post: xSicKxBot
Today 10:29 AM
» Replies: 0
» Views: 6
News - Don’t Miss: Designing for seasons...
Last Post: xSicKxBot
Today 10:29 AM
» Replies: 0
» Views: 9

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020