Creating objects on the fly in Python

Why do i need

class DummyObject(object): pass 

to make

 dummy = DummyObject() dummy.foo = 42 

Why is this not legal in Python?

 dummy = object() dummy.foo = 42 

Which gives an error:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'object' object has no attribute 'foo' 
+7
python
source share

No one has answered this question yet.

See similar questions:

66
Unable to set feature class attributes

or similar:

5504
Does Python have a ternary conditional operator?
5231
What are metaclasses in Python?
4473
Calling an external command in Python
3790
How can I safely create a subdirectory?
3602
Does Python have a "contains" substring method?
3119
What is the difference between Python list methods that are added and expanded?
2818
Finding an index of an element with a list containing it in Python
2601
How can I make a time delay in Python?
2568
How to find out the current time in Python
814
Creating a singlet in Python

All Articles