Python IsInstance() for classes and subclasses
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2
--
Chapters
00:00 Question
02:37 Accepted answer (Score 2)
04:25 Thank you
--
Full question
https://stackoverflow.com/questions/5038...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #class #multipleinheritance #pythondecorators
#avk47
ACCEPTED ANSWER
Score 2
If SizedString is an instance of Descriptor ( based on Inheritance hierarchy- String , Typed , MaxSized, Descriptor ), then String and Integer also should satisfy the If condition right ? because at the end it is also the subclass of ( typed , Descriptor ) ?
We have to look carefully at what is being passed into the
check_attributesfunction. Look closer at what the value of thenameandsharekeyword arguments are:@check_attributes(name=String,shares=Integer,place=SizedString('tester',size=8))Notice the lack of parenthesis after the
StringandIntegerclass names? This means theStringandIntegerclass object themselves are being passed intocheck_attributes, not an instance of either class. And since theStringclass object and theIntegerclass object are not sub-classes ofDescriptor,isinstance(value, Descriptor)fails.What is value(key) in setattr(cls, key, value(key)) means , cant understand what is value(key ) means ?
Think about it. Since
valuehas the value of whatever keyword argument was passed intocheck_attributes, and the value is not an instance of theDescriptorclass, thenvaluemust be referring to a class object. (If you don't understand why this is, refer back to my answer to your first question). So callingvalue(key)is creating an instance of some class, and passing in thekeyvalue as a constructor argument.