3 ms·
> The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific in
by _gabe_ 3y ago
> The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__(), like this:
def __init__(self):
self.data = []
> When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly created class instance. So in this example, a new, initialized instance can be obtained by:
x = MyClass()
Seems like a good explanation to me? Maybe people are upset that it's buried a bit in the docs instead of having a tiny out of context statement about it?
[0]: https://docs.python.org/3/tutorial/classes.html https://docs.python.org/3/tutorial/classes.html
- dageshi 3y agoYes, I suspect people saw a wall of text and went back to look for a shorter answer. I would guess google A/B tested those results till python.org fell off the front page. More to the point, I don't think either google or the people searching are wrong for that. The query isn't "teach me about python classes" it's "what is python __init__?".
- _gabe_ 3y agoYea, I used to get annoyed when I saw results like this. But I appreciate them more and more. It's still easy to find what you're looking for with a quick Ctrl+F. But there inevitably comes a time when I want to understand a concept deeper, and I can come back to results like this for the longer explanation. I know that it doesn't have to be either a long explanation or a quick and concise snippet of information, it can be both. But I find that a lot of docs will either have long explanations or short snippets (because time is finite and doing both is duplicating efforts), and in those cases, I prefer the long explanation.