Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python issubclass()

#1
Python issubclass()

Python’s built-in issubclass(X, Y) function takes a class X and a class Y and returns True if the X is an instance of Y and otherwise False. The argument Y can also be a tuple in which case it checks whether X is a subclass of any class in the tuple—such as in issubclass(X, (class_1, class_2, ...)).

Python issubclass() Function - Example + Definitionhttps://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w" sizes="(max-width: 768px) 100vw, 768px" />

Usage


Learn by example! Here’s an example on how to use the issubclass() built-in function.

class Car: pass class Porsche(Car): speed = 200 #mph print('Is Porsche a subclass of Car?')
print(issubclass(Porsche, Car))

The output is:

Is Porsche a subclass of Car?
True

Porsche is a subclass of Car because you set the inheritance hierarchy in the class definition with Porsche(Car). Porsche inherits ever method and attribute from its parent Car—so the function issubclass(Porsche, Car) returns True.

If you pass a non-parent of Porsche, the function will return False:

print(issubclass(Porsche, int))
# False

Video issubclass()




Syntax issubclass()


The issubclass() method has the following syntax:

Syntax: 
issubclass(class, class) # Check if a class is a subclass of another class. issubclass(class, (class_A, class_B, ...)) # Check if a class is a subclass of any of multiple other classes.

Arguments class Class to be checked for being a subclass.
class or tuple of classes Class or tuple of classes the first argument should be checked against.
Return Value Boolean Returns whether the class is a subclass of any of the classes given in the second argument.

Return value from issubclass()


The issubclass(class, classes) method returns True if the class is a subclass of any of the classes given in the second argument. Otherwise, it returns False.

Interactive Shell Exercise: Understanding issubclass()


Consider the following interactive code:

Exercise: One of the outputs is False. Which one?


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


Python issubclass() vs isinstance()


Python has two similar but different built-in functions issubclass() and isinstance().

  • The function issubclass() checks whether a class is a subclass of another class.
  • The function isinstance() checks if an object is an instance of a class.

So, the difference between issubclass() and isinstance() is that the former is about the relationship between two classes and the latter is about the relationship between an instance and a class.

Here’s an example:

class Car: pass class Porsche(Car): speed = 200 #mph print(issubclass(Porsche, Car))
# True your_car = Porsche()
print(isinstance(your_car, Car))
# True1

Summary


Python’s built-in issubclass(X, Y) function takes a class X and a class Y and returns True if the X is an instance of Y and otherwise False.

class Car: pass class Porsche(Car): speed = 200 #mph print(issubclass(Porsche, Car))
# True

The argument Y can also be a tuple in which case it checks whether X is a subclass of any class in the tuple—such as in issubclass(X, (class_1, class_2, ...)).

print(issubclass(Porsche, (int, float, Car)))
# True

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python issubclass() first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...ssubclass/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Compare Rpm Versions in Python? xSicKxBot 0 15 03-01-2021, 07:10 PM
Last Post: xSicKxBot
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 25 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 41 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 33 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 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
  [Tut] How To Cut A String In Python? xSicKxBot 0 35 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python filter() xSicKxBot 0 56 02-01-2021, 03:25 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) PAW Patrol, Ben10, Dead in ...
Last Post: xSicKxBot
Yesterday 11:22 PM
» Replies: 0
» Views: 0
News - You Can Now Play As Xenoblade’s P...
Last Post: xSicKxBot
Yesterday 11:21 PM
» Replies: 0
» Views: 0
News - A Quiet Place 2 Release Moves For...
Last Post: xSicKxBot
Yesterday 11:21 PM
» Replies: 0
» Views: 0
Mobile - Kick the Buddy guide – tips, tr...
Last Post: xSicKxBot
Yesterday 04:11 PM
» Replies: 0
» Views: 2
News - This Week At Bungie – 3/4/2021
Last Post: xSicKxBot
Yesterday 03:07 PM
» Replies: 0
» Views: 1
Xbox Wire - Horned Knight: Reintroducing...
Last Post: xSicKxBot
Yesterday 03:04 PM
» Replies: 0
» Views: 1
News - Which genre should your next PC g...
Last Post: xSicKxBot
Yesterday 03:03 PM
» Replies: 0
» Views: 5
News - One month in, Valheim has already...
Last Post: xSicKxBot
Yesterday 03:03 PM
» Replies: 0
» Views: 4
[1.07] BO4 Zombie Trainer (Semi-Patched)
Last Post: Appledryck
Yesterday 09:39 AM
» Replies: 13
» Views: 513
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: Appledryck
Yesterday 09:37 AM
» Replies: 55
» Views: 2546

[-]
Twitter

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

Copyright © SickGaming.net 2012-2020