thatisvivek
thatisvivek

Reputation: 915

Python 3 built-in functions and classes

When I am creating a list using below statement

a = list('jane')

Am I calling Python's built-in list function or instantiating list class.

My understanding is we are instantiating list class by passing 'jane' as argument.

However, the Python's documentation https://docs.python.org/3/library/functions.html says list() is built-in function.

Upvotes: 0

Views: 113

Answers (3)

Soviut
Soviut

Reputation: 91555

You are instantiating a list.

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.

Upvotes: 0

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160437

The docs explicitly say:

class list([iterable])

Rather than being a function, list is actually a mutable sequence type

You can easily check that:

>>> type(list)
type

if it was a function, function would be the output provided by using type.

You're instantiating a list object the same way you'd do if you created your own class and called it. type's __call__ is essentially getting invoked and sets up your instance so, though they aren't a function per se, they are callable.


The fact that they are listed in that specific section is probably for convenience, it might be confusing but reading the description of it is supposed to disambiguate this.

Upvotes: 1

phihag
phihag

Reputation: 287865

Your question is answered by the very documentation page you mention:

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.

In Python, both classes and functions are callable, so in practice, you can treat them alike.

Upvotes: 1

Related Questions