Is it a class or a function?
Briefly

Is it a class or a function?
"In Python, it's not always easy to tell the difference between a class and a function... at least not from the perspective of a user of that class or a function. Classes are callable in Python To create a new instance of a class in Python, we call the class: Calling a class returns a new instance of that class: That is, an object whose type is that class:"
"Is print a class or a function? Pause and take a guess. ... What do you think? Is print a class or a function? It turns out... It's a function! That kind of makes sense. We don't get a new instance of a print object back when we call the print function. Instead, the print function does a thing for us. Functions often do a thing. But sometimes, they return a thing."
"What about str? Is str a class or a function? Again, pause and take a guess. ... It turns out that str... ... is a class! When we call the str function, we get back a new string object."
Classes in Python are callable; calling a class returns a new instance of that class, an object whose type is that class. The syntax for calling a function and creating a new class instance is the same: both use parentheses. Functions run code and often perform actions, sometimes returning values. Classes are called to construct new objects such as strings or lists. Built-in examples show print and sum behave as functions that perform operations, while str and list behave as classes that produce new objects. Both 'function' and 'class' terminology are used for some callables in Python.
Read at Pythonmorsels
Unable to calculate read time
[
|
]