2

Can we create and edit instance of object class in Python? I have written following code:

req=object()
setattr(req,'member1','value1')

But it shows an error as

AttributeError: 'object' object has no attribute

Am I missing something?

gliese581g
  • 433
  • 8
  • 21
  • 1
    In general built-in types do *not* allow setting attributes no them. If you try with `list`, `tuple` etc. If you are using python3.3+ you can use [`types.SimpleNamespace`](http://docs.python.org/3/library/types.html#types.SimpleNamespace) to have objects where you can set attributes. (By the way: I don't understand why the OP used `setattr` at all. If you have a fixed attribute name you can just do: `req = object(); req.member1 = 'value1'`). – Bakuriu Jan 28 '14 at 08:33
  • [python create object and add attributes to it](http://stackoverflow.com/questions/2827623/python-create-object-and-add-attributes-to-it) - I guess, this could help you. It's about similar problem. – Marek Jan 28 '14 at 08:35

0 Answers0