python - How can I create an object and add attributes to it?

ID : 7532

viewed : 67

Tags : pythonclassobjectattributespython





Top 5 Answer for python - How can I create an object and add attributes to it?

vote vote

100

The built-in object can be instantiated but can't have any attributes set on it. (I wish it could, for this exact purpose.) It doesn't have a __dict__ to hold the attributes.

I generally just do this:

class Object(object):     pass  a = Object() a.somefield = somevalue 

When I can, I give the Object class a more meaningful name, depending on what kind of data I'm putting in it.

Some people do a different thing, where they use a sub-class of dict that allows attribute access to get at the keys. (d.key instead of d['key'])

Edit: For the addition to your question, using setattr is fine. You just can't use setattr on object() instances.

params = ['attr1', 'attr2', 'attr3'] for p in params:     setattr(obj.a, p, value) 
vote vote

88

You could use my ancient Bunch recipe, but if you don't want to make a "bunch class", a very simple one already exists in Python -- all functions can have arbitrary attributes (including lambda functions). So, the following works:

obj = someobject obj.a = lambda: None setattr(obj.a, 'somefield', 'somevalue') 

Whether the loss of clarity compared to the venerable Bunch recipe is OK, is a style decision I will of course leave up to you.

vote vote

75

There is types.SimpleNamespace class in Python 3.3+:

obj = someobject obj.a = SimpleNamespace() for p in params:     setattr(obj.a, p, value) # obj.a.attr1 

collections.namedtuple, typing.NamedTuple could be used for immutable objects. PEP 557 -- Data Classes suggests a mutable alternative.

For a richer functionality, you could try attrs package. See an example usage.

vote vote

63

The mock module is basically made for that.

import mock obj = mock.Mock() obj.a = 5 
vote vote

55

You can also use a class object directly; it creates a namespace:

class a: pass a.somefield1 = 'somevalue1' setattr(a, 'somefield2', 'somevalue2') 

Top 3 video Explaining python - How can I create an object and add attributes to it?







Related QUESTION?