Is there a Python shortcut for an __init__ that simply sets properties?

You might find the attrs library helpful. Here's an example from the overview page of the docs:

>>> import attr
>>> @attr.s
... class SomeClass(object):
...     a_number = attr.ib(default=42)
...     list_of_numbers = attr.ib(factory=list)
...
...     def hard_math(self, another_number):
...         return self.a_number + sum(self.list_of_numbers) * another_number
>>> sc = SomeClass(1, [1, 2, 3])
>>> sc
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
>>> sc.hard_math(3)
19

If you were using Python 3.8+, you could use dataclasses.

>>> from typing import List
>>> from dataclasses import dataclass, field
>>> @dataclass
... class OtherClass:
...     a_number: int=42
...     list_of_numbers: List[int] = field(default_factory=list)
...     def hard_math(self, another_number):
...         return self.a_number + sum(self.list_of_numbers) * another_number
>>> OtherClass=SomeClass
>>> oc = OtherClass(1, [1, 2, 3])
>>> oc
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
>>> oc.hard_math(3)
19

Sure.

Class SomeClass(object):
    def __init__(self, **args):
        for(k, v) in args.items():
            setattr(self, k, v)

And v = SomeClass(a=1, b=2, c=3, d=4)

Bug it would make your code hard to understand.

Good Luck.


You could use Alex Martelli's Bunch recipe:

class Bunch(object):
    """
    foo=Bunch(a=1,b=2)
    """
    def __init__(self, **kwds):
        self.__dict__.update(kwds)

You can make a class with a __new__ method that copies any class properties to to object, then inherit from that.

http://www.oreilly.com/programming/free/how-to-make-mistakes-in-python.csp has an example of why what I just said is a terrible idea that should be avoided.

(Short version: it doesn't work well with mutable objects, and the work-arounds for dealing with that are not worth the effort.)