Beginner question

S

Steven D'Aprano

Sorry for the delay in replying.


[1] Technically it's a type, not a function, but the difference makes
no difference here.
Can you explain me the difference of the type and function you've just
mentioned?

We were talking about dict().

In Python, "type" is another name for "class". There is a built-in class
called "dict":

py> dict
<class 'dict'>

The way we create a new instance of a class is to call it, as if it were
a function:

py> dict()
{}

just like you might call some other function:

py> len([])
0


so sometimes it is convenient to be lazy and just refer to the type/class
as a function. The general term for things which can be called in Python
is "callable", which includes functions, methods, and types.

(Back in the ancient days of Python 1.x, dict *actually was a function*,
just like len() or ord(), and the type/class system was radically
different. But that's ancient history now.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,795
Members
47,342
Latest member
eixataze

Latest Threads

Top