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?

2
  • 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 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'). Commented Jan 28, 2014 at 8:33
  • python create object and add attributes to it - I guess, this could help you. It's about similar problem. Commented Jan 28, 2014 at 8:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.