+ 1

type() is not a function?

Python `type` is an object of class `type`. Same with int, str, map etc. https://code.sololearn.com/cCKy3vfBMzvI/

17th Oct 2020, 8:04 AM
Ore
Ore - avatar
13 odpowiedzi
+ 6
Ore... I had similar questions about this a while back and was disappointed by the lack of clarification in the official documentation. I decided to review the C and Python source code directly and made the following assessment. IIRC, these built-in functions are special functions that map directly to C functions. Therefore, technically, these are the most fundamental functions for which all other functions and classes in Python are built on. For this reason, built-in C functions, like str(), int(), type(), etc, build objects as if they're class constructors and will resolve as <class 'type'>. While these built-in C functions might seem more like Python class constructors, perhaps it's the other way around. That is, I speculate the interface for creating objects in Python is modeled after these built-in C functions. Makes you wonder... 😉 Also, you might be interested in my version of your code, which I created some time ago on my local computer: https://code.sololearn.com/c26xp4s2z6zk/
18th Oct 2020, 4:58 AM
David Carroll
David Carroll - avatar
+ 1
Why does the documentation refer to them as functions when they are, in fact, constructors? I see it was explained clearly down the page but the title "built-in functions" is misleading. https://docs.python.org/3/library/functions.html
17th Oct 2020, 8:43 AM
Ore
Ore - avatar
+ 1
Slick If you look closely, you will notice that some of the built-ins where prefixed with 'class' e.g ''' class bool([x]) ''' These are constructors and have a type of <class "type"> Somewhere down the page you will find this ''' class list([iterable]) Rather than being a function, list is actually a mutable sequence type, as documented in Lists and Sequence Types — list, tuple, range. ''' Conclusion - not all the built ins are functions. Which explains our previous findings. I just wish the documentation made this more obvious from the start.
17th Oct 2020, 8:55 AM
Ore
Ore - avatar
+ 1
I think type is the class of classes.
17th Oct 2020, 10:09 AM
Seb TheS
Seb TheS - avatar
+ 1
Ore Yes. isinstance(str, type) ---> True
17th Oct 2020, 12:57 PM
Seb TheS
Seb TheS - avatar
+ 1
David Carroll Thanks for the answer. I will check the code when I have time.
18th Oct 2020, 5:20 AM
Ore
Ore - avatar
0
makes sense since python is so uppity about EVERYTHING is a class (or part of one) funny find though.
17th Oct 2020, 8:10 AM
Slick
Slick - avatar
0
Slick I think you meant that everything is an object. Yes. That is true but functions are also object. So `type` would still be an object if it was a function.
17th Oct 2020, 8:24 AM
Ore
Ore - avatar
0
I just checked as well that is a bit weird. Then i tried type checking "all" and it comes out as what's expected (function or built in method)! I think it may be how each is made induvidually
17th Oct 2020, 8:50 AM
Slick
Slick - avatar
0
Seb TheS I don't understand. Are you saying every class is an instance of `type`? 😲
17th Oct 2020, 12:15 PM
Ore
Ore - avatar
0
Seb TheS It's all too confusing for me 🧐
17th Oct 2020, 1:29 PM
Ore
Ore - avatar
0
type is like the base object. Similar to Object in JavaScript or stdClass in PHP?! I am trying to compare this with what I already know.
17th Oct 2020, 1:31 PM
Ore
Ore - avatar