Search code examples
pythonpython-class

Python - init argument not used in self


I came by a code and wanted to get more understanding on when to use such arrangement. What would be a good scenario of not using __init__'s argument (name) in self?

class ArgumentNotUsedInSelf:
    def __init__(self, name: str):
        self.type = "user"

user_one = ArgumentNotUsedInSelf("Mike")
print(user_one.type)

Any explanations from architectural (and not only) point of view are most welcome!


Solution

  • Some of the reasons for this can be:

    • Historical. The class used to use the argument, but it was changed so it's no longer meaningful. The argument was kept for backward compatibility.
    • Class hierarchy. This may be a child class of a class that uses the argument, but the child overrides the need for it. The argument is required for compatibility with the parent.