+ 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/
13 RĂ©ponses
+ 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/
+ 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
+ 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.
+ 1
I think type is the class of classes.
+ 1
Ore Yes. isinstance(str, type) ---> True
+ 1
David Carroll Thanks for the answer. I will check the code when I have time.
0
makes sense since python is so uppity about EVERYTHING is a class (or part of one) funny find though.
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.
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
0
Seb TheS I don't understand. Are you saying every class is an instance of `type`? đČ
0
Seb TheS It's all too confusing for me đ§
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.