Beginner question

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jun 6 22:21:22 EDT 2013


Sorry for the delay in replying.


On Tue, 04 Jun 2013 15:51:38 +0300, Carlos Nepomuceno wrote:

>> [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.)



-- 
Steven



More information about the Python-list mailing list