I am developing a Python application where I need many times to check if an object is a subclass of the database model.
I made my own function for this:
def isModel(obj): return isinstance(obj, type) and issubclass(obj, Model)
issubclass throws an obj exception is not a class, but I would like it to simply return False if obj not a class.
I thought it was better to make another function, instead of using the built-in issubclass :
def _issubclass(obj, Klass): return isinstance(obj, type) and issubclass(obj, Klass)
But why the built-in issubclass not created like that? What reason? Did I miss something?
UPDATE:
I have models:
class BaseModel(object): id = Field(...) class MyModel(BaseModel): deleted = Field(...)
In the function, I want to check if the argument is BaseModel :
def update_model(model): assert isinstance(model, type) and issubclass(model, BaseModel), 'Must be a model'
issubclass answers the question of whether the object is a subclass of this class. If the object is an instance of the class, so the IMO response should be "No, your object is not a subclass of BaseModel because it is not a class at all."
In Python, itβs quite normal to use if something is not None or len(something) != 0 instead of if something is not None or len(something) != 0 and not raise any TypeError . What is the usefulness of raising a TypeError if the first issubclass argument issubclass not a class?
For example, someone asks a dog: βDo you solve this problem correctly?β, And instead of saying βNoβ, the dog says: βI'm not a man.β I asked someone one (subclass), and he did not answer my question.