Home > Mobile >  How do I create an object with subvalues without creating a class?
How do I create an object with subvalues without creating a class?

Time:11-20

I want to create an object, A, with x and y values without creating a class.

#<Code I am looking for goes here.>

print(A.x, A.y)

Is there an easy way to do this that I am missing, or is it too hacky?

CodePudding user response:

I found an answer to this question:

A = type('any name', (), {'x': 15, 'y': 23})

print(A.x, A.y)

Read more about it here.

CodePudding user response:

Another way to accomplish this would be:

import types

A = types.SimpleNamespace(x=5, y=2)
print(A.x, A.y)
  • Related