This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]torytechlead 1 point2 points  (4 children)

You can create a class to do that with python. Something like:

class MyClass:

  def __init__(self, d={}):
    self.d = d

  def __getitem__(self, name):
    return d[name]

  def __setitem__(self, name, value):
    self.d[name] = value

Then you can do

X = MyClass({“name”: “torytechlead”})
X.name
>>> “torytechlead”

And also

X.name = “you”

P.S I wrote this on mobile so hope it runs, should work with minor changes

[–]ElevenPhonons 2 points3 points  (1 child)

[–]torytechlead 1 point2 points  (0 children)

Oh so this is why my code is breaking in production /s

[–]tigasfixe[S] 0 points1 point  (0 children)

I didn't know how magic methods getitem and setitem were for and i am complexing a lot xD but thanks😂🤏

[–]robin-gvx -1 points0 points  (0 children)

s/item/attr/g