1

Say I have this code:

class B:
    def __init__(self, **fields):
        self.__dict__.update(fields)


class A(B):
    def __init__(self, **fields):
        super().__init__(**fields)

What is going on here? Why are updating the __dict__ of B in this way?

So if we instantiated A(c=1, d=2), what happens?

martineau
  • 119,623
  • 25
  • 170
  • 301
Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • Is `ABC` referrring to `abc.ABC` in metaclasses? It updates every keyword argument in the dictionary as an attribute of an instance, so `a.__dict__ = {'c': 1, 'd': 2}` – Devesh Kumar Singh Jul 17 '19 at 19:17
  • 1
    It sets instance attributes for each keyword argument. Also: it's ugly and bad, don't use it. – wim Jul 17 '19 at 19:18
  • 1
    [Is self.__dict__.update(**kwargs) good or poor style?](https://stackoverflow.com/q/9728243/674039) – wim Jul 17 '19 at 19:19
  • if one of you writes a full answer, I'll give credit! – Jwan622 Jul 17 '19 at 19:23
  • 1
    I added the answer by wim as a dupe, since it already addresses what's happening here, which is what the question is about – Devesh Kumar Singh Jul 17 '19 at 19:27

0 Answers0