Question

I have countless Python classes from various projects from SQLAlchemy (and a couple from Pygame as well), and I recently noticed a pattern in many of them: their constructors always went something like this:

class Foo(Base):
    def __init__(self, first, last, email, mi=""):
        self.first = first
        self.last = last
        self.email = email
        self.mi = mi

... whereby the only thing the constructor did was to transfer a set of positional arguments into an exactly identically named set of data members, performing no calculation or other function calls whatsoever.

It seems to me that this repetition is unnecessary and prone to human error upon change.

This leads me to the question here: is it possible to automatically generate such an __init__(self, ...) function, preferably without mucking around with CPython bytecode or using templates/macros to alter the source file itself?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top