I was learning about enumerate(). While learning, when I used,
help(enumerate)
Help on class enumerate in module builtins:
class enumerate(object)
| enumerate(iterable, start=0)
|
| Return an enumerate object.
|
| iterable
| an object supporting iteration
|
| The enumerate object yields pairs containing a count (from start, which
| defaults to zero) and a value yielded by the iterable argument.
|
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), …
|
| Methods defined here:
|
| getattribute(self, name, /)
| Return getattr(self, name).
|
| iter(self, /)
| Implement iter(self).
|
| next(self, /)
| Implement next(self).
|
| reduce(…)
| Return state information for pickling.
Static methods defined here:
new(*args, **kwargs) from builtins.type
Create and return a new object. See help(type) for accurate signature.
Even when I gave as,
enumerate
<class ‘enumerate’>
But, when I checked the documentation in the official website of python, www.python.org for enumerate()
It showed that enumerate() is a function which violated the information shown by help().
I couldn’t get whether enumerate() is a class or a function. Anyone please help me out of this please…
By the way, I had python 3.8.3. I even checked in python 3.6 and 3.7.10.
CodePudding user response:
One notable thing about Python is that there is no distinct syntax to create an object, by instantiating a class, and calling a function.
Actually, under the hood, on the correct abstraction layer, what both things do is to run the __call__
method on the object preceding the (...)
used to pass arguments.
The "enumerate" call, anyway, would have to return an object able to be iterated so that it can be used in for
loop constructs.
Technically, the documentation where you saw "enumerate is a function" is incorrect - and you could even file a bug for it indicating it should indicate it is a class instead. In practice, when writting documentation and tutorial for beginners in the language, they will have little gain in knowing, at that point, that things like "enumerate", "range", "bool" are classes themselves and other calls like "open", "iter" are functions that return objects they instantiate.
CodePudding user response:
enumerate
is a class in python which returns an enumerate object.
print(enumerate) --> <class 'enumerate'>
If it was a function you would see something like <built-in function enumerate>
.
Probably you saw this page , which has a title of "built-in functions" and you thought maybe it's a function. No it's not. In that list there both functions (like abs
, ...) and classes/types (like int
, ...) listed.
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.
Every classes in python are types.