Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6991967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:34:25+00:00 2026-05-27T19:34:25+00:00

I am developing a Python application where i need many times to check if

  • 0

I am developing a Python application where i need many times to check if an object is a subclass of a DB model.

I did my own function to do that:

def isModel(obj):
    return isinstance(obj, type) and issubclass(obj, Model)

issubclass raises exception of obj is not a class, but i would like it just return False if obj is not a class.

I thought better to make another function, to use instead of the built-in issubclass:

def _issubclass(obj, Klass):
    return isinstance(obj, type) and issubclass(obj, Klass)

But why the built-in issubclass was not made like that? What’s the reason? Am i missing something?


UPDATE:

I have models:

class BaseModel(object):
    id = Field(...)

class MyModel(BaseModel):
    deleted = Field(...)

In a function i want check if an argument is a BaseModel:

def update_model(model):
    assert isinstance(model, type) and issubclass(model, BaseModel), 'Must be a model'

issubclass answers the question if an object is a sub-class of the given class. If the object is a class instance, so the answer, IMO, should be ‘No, your object is not a BaseModel subclass, because it’s not a class at all’.

In Python is quite normal instead of if something is not None or len(something) != 0 to use if something and not raising any TypeError. What’s the usefulness of raising TypeError if the first argument of issubclass is not a class?

For example someone asks a dog: ‘Are you the right man to solve this task?’, and instead of answering ‘No’, the dog says ‘I am not a man’. I asked someone one thing (is sub class) and he didn’t answer my question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:34:26+00:00Added an answer on May 27, 2026 at 7:34 pm

    Isinstance will probably do what you want. isinstance(obj, Klass) will return True when obj is an instance of a class that is a subclass of Klass.
    Example:

    >>> class A(object):
    ...    pass
    ... 
    >>> class B(A):
    ...    pass
    ... 
    >>> obj = B()
    >>> 
    >>> isinstance(obj, A)
    True
    

    “What’s the usefulness of raising TypeError if the first argument of issubclass is not a class?”

    Explicit is better than implicit. You passed a non-class into issubclass. That indicates that you are doing something wrong. Therefore you get an error. If you seriously do want to pass instances to issubclass, you can catch that error:

    try:
        isbasemodel = False
        if issubclass(x, BaseModel):
           isbasemodel = True
    except TypeError:
        # Accept that we test non-classes:
        pass
    

    Or, of course, you can do what you do: An extra check.

    However, if it did not raise an error, then it would be impossible to catch the mistake of passing in an instance.

    From what I understand, you want to make a check in an API or similar, that the object passed in is a subclass of BaseModel, and the user should not be allowed to pass in an instance. IMO, the correct thing to do there is to raise a TypeError if they pass in the wrong thing. Like so:

    def update_model(model):
        if not issubclass(model, BaseModel):
            raise TypeError('Must be a model')
    

    Making an assert there just means that you hide the fact that it is a type error to pass in the wrong thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a GPL-licensed application in Python and need to know if the
I'm developing an application that uses webservices in python, both sides (server and client)
I'm developing a python application and have a question regarding coding it so that
I am developing a library and an application that uses the library in Python
I'm developing a Python/ObjC application and I need to call some methods in my
I am currently developing an application in python 3 and i need to be
i am developing an application using python that requires to be notified when the
I'm developing web application on python. But the client wants a tool that exports
i am using python for developing Google app engine application. my question is that
I'm developing an application with PyGTK that will make use of visual-python's 3d drawings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.