Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Why are classes listed in the list of built-in functions?

Tags:

python

The Python docs lists property() as a built-in function.

However, the function description has the keyword "class" in front of it in the docs.

class property(fget=None, fset=None, fdel=None, doc=None)

This also happens with

class set([iterable])

and

class slice(stop)

What does this mean? - why are classes listed under built-in functions. Is this just a documentation issue or is there a technical reason?

EDIT: I am not asking about how property() works.

like image 260
Kevin L. Avatar asked Jan 25 '18 09:01

Kevin L.


People also ask

Is list a built in class in Python?

list is a built-in class in Python. However, classes are callable just like functions, and when called, classes instantiate and return objects, so you can pass a class as an argument where a function (or a callable to be precise) is expected.

What are built in functions for list?

Compares elements of both lists. Returns item from the list with max value. Returns item from the list with min value. Converts a tuple into list.

What happens when you use the built in function any on a list in Python?

Python any() The any() function returns True if any element of an iterable is True . If not, it returns False .


1 Answers

The Python glossary defines a function as:

A series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body.

A class can be passed arguments, and returns a value to the caller, so arguably by this definition classes are functions*.

In addition (as deceze points out in the comments), a class should always return an instance of itself – set, property, slice, etc. all return instances of set, property, slice, etc. respectively – so set, property and friends are all also classes, and so they are documented as such:

class set([iterable])

meaning that set is a class, not that it returns one.

I would guess that set etc. are documented in the "built-in functions" page because a) they are callable, and b) it's convenient to have all the documentation for "things you can call" in one place.

*Strictly speaking, isinstance(C, types.FunctionType) is false for any class C (as far as I can tell), but classes are certainly callables (isinstance(C, typing.Callable) is true), which is maybe a more useful property to think about.

like image 77
ash Avatar answered Oct 20 '22 21:10

ash