views:

85

answers:

2

What I want to do in my code:

myobj = <SomeBuiltinClass>()
myobj.randomattr = 1
print myobj.randomattr
...

I can implement a custom SomeClass that implements __setattr__ __getattr__. But I wonder if there is already a built-in Python class or simple way to do this?

+2  A: 

I like using the Bunch idiom for this. There are list of variations and some discussion here.

thrope
Excellent link. Thanks.
Evgenyt
+6  A: 

You can just use an empty class:

class A(object): pass

a = A()
a.randomattr = 1
truppo
Good to know. But it there already an empty class in Python library?
Evgenyt
No, there no built-in empty class. You can define an empty class in a single line of code, as shown. The one-liner is as short as an `import` statement, so there's no point in looking for a built-in empty class.
S.Lott