1
a = None
a.s = 2

throws an AttributeError. Is there a possibility to create an object a with the field s with value 2 without creating an own class for that? Would be super cool if Python would allow that hack! 8)

duesterdust
  • 87
  • 1
  • 7
  • Python has builtins that would allow you to do this, but why not just make your own class? Just `class NameSpace: pass; a = NameSpace(); a.s = 2` – juanpa.arrivillaga Nov 29 '19 at 07:44
  • 2
    Honestly, that target duplicate is quite old, I wouldn't use a function object for that. You can use `from types import SimpleNamespace; a = SimpleNamespace(); a.s = 2` or even `a = SimpleNamespace(s=2)` – juanpa.arrivillaga Nov 29 '19 at 07:49
  • @juanpa.arrivillaga Agreed, I added another target with more up-to-date answers. – user2390182 Nov 29 '19 at 07:52

0 Answers0